How to get first Monday of month in JavaScript?

Are you looking for a way to get the first Monday of a month using JavaScript?

Look no further! In this article, we’ll show you how to do it with easy-to-follow code examples and explanations.

Keep reading to master this useful skill and learn how to easily find the first Monday of any month using JavaScript.

Solutions on how to get the first Monday of the Month in JavaScript

Here the following solutions to get first Monday of the Month in JavaScript.

Solution 1

This function takes a date string as an argument and returns the date of the first Monday of the month.

Here’s the example code where you can use to get the get first Monday of month in JS:

function findFirstMonday(dateString) {
  let targetDate = new Date(dateString);
  let targetMonth = targetDate.getMonth();
  let targetYear = targetDate.getFullYear();
  let firstDateInMonth = new Date(targetYear, targetMonth, 1);
  let firstWeekdayInMonth = firstDateInMonth.getDay();
  let firstMondayDate = 1 + ((8 - firstWeekdayInMonth) % 7);
  return new Date(targetYear, targetMonth, firstMondayDate).toLocaleDateString();
}

console.log(findFirstMonday("1 July 2023"));

This function accepts a date string as input and determines the date of the first Monday in that month.

It accomplishes this by creating a new Date object representing the first day of the specified month.

Using the getDay() method and some calculations, it determines the date of the first Monday.

The function then returns a formatted date string for that particular date.

For instance, if you invoke the function with the argument “1 July 2023”, it will create a Date object for July 1, 2023.

Since July 1, 2023 falls on a Saturday, the function calculates that the first Monday in July 2023 is July 3. Subsequently, it returns a formatted date string representing July 3, 2023.

Output:

7/3/2023

Solution 2

This function takes a year and a month as arguments and returns a Date object representing the first Monday of the specified month.

Here’s another example code:

function getFirstMonday(year, month) {
  let date = new Date(year, month - 1, 1);
  while (date.getDay() !== 1) {
    date.setDate(date.getDate() + 1);
  }
  return date;
}

let firstMonday = getFirstMonday(2023, 7);
console.log(`The first Monday of the month is: ${firstMonday.toLocaleDateString()}`);

The solution I provided is a function that takes a year and a month as arguments and returns a Date object representing the first Monday of the specified month.

The function creates a new Date object representing the first day of the specified month, then uses a while loop to increment the date until it falls on a Monday.

Once the date falls on a Monday, the function returns the Date object representing that date.

Output:

The first Monday of the month is: 7/3/2023

How to get the first Monday of a month using moments.js?

You can use the moment.js library to find the first Monday of a month. Here’s an example of how you can do it:

const moment = require('moment');

function getFirstMonday(dateString) {
  let date = moment(dateString, 'YYYY-MM-DD');
  let firstMonday = date.startOf('month').day("Monday");
  if (firstMonday.date() > 7) firstMonday.add(7, 'd');
  return firstMonday.format('L');
}

console.log(getFirstMonday('2023-07-01'));

This function takes a date string in the format YYYY-MM-DD as an argument and returns the date of the first Monday of the month.

For instance, getFirstMonday(‘2023-07-01’) would return 07/03/2023.

Output:

07/03/2023

How to get first Monday and last Monday of the month?

If you wanted to get the first day Monday and last Monday of the month here’s the example code:

function getFirstAndLastMonday(dateString) {
  let targetDate = new Date(dateString);
  let targetMonth = targetDate.getMonth();
  let targetYear = targetDate.getFullYear();
  let firstDateInMonth = new Date(targetYear, targetMonth, 1);
  let firstWeekdayInMonth = firstDateInMonth.getDay();
  let firstMondayDate = 1 + ((8 - firstWeekdayInMonth) % 7);
  let lastDateInMonth = new Date(targetYear, targetMonth + 1, 0);
  let lastWeekdayInMonth = lastDateInMonth.getDay();
  let lastMondayDate = lastDateInMonth.getDate() - ((lastWeekdayInMonth + 6) % 7);
  return {
    firstMonday: new Date(targetYear, targetMonth, firstMondayDate).toLocaleDateString(),
    lastMonday: new Date(targetYear, targetMonth, lastMondayDate).toLocaleDateString()
  };
}

console.log(getFirstAndLastMonday("1 july 2023")); 

This function takes a date string as an argument and returns an object with the date of the first and last Monday of the month. For example, getFirstAndLastMonday(‘1 jul 2023’) would return {firstMonday: “7/3/2023”, lastMonday: “7/31/2023”}.

Output:

{ firstMonday: '7/3/2023', lastMonday: '7/31/2023' }

Conclusion

In conclusion, this article presented various solutions to get the first Monday of a month in JavaScript.

By following the provided code examples and explanations, you can quickly and easily implement these solutions to obtain the desired result.

Whether it’s using basic JavaScript functions or leveraging external libraries like moment.js, JavaScript offers flexible approaches to calculate the first Monday of any given month.

With this newfound knowledge, you can efficiently work with dates and incorporate this functionality into their applications as needed.

We are hoping that this article provides you with enough information that helps you understand the JavaScript get first Monday of month.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Leave a Comment