7
1783

How to Get Date of Next Monday in JavaScript

Reading Time: 2 minutes

There are 2 ways to easily get date of next Monday in JavaScript. Let’s explore the methods.

Methods to Get Date of Next Monday in JavaScript

Here are the list of 2 methods where we can get date of next Monday in JavaScript.

  • Next Monday Calculation Method
  • Array Search Method

Let’s look into each details with illustration below.

1. Next Monday Calculation Method

We will calculate out the number of days till next Monday, then sum to the current date using getDay() method.

Process Flow:

  1. Clone the date so the original date will not get modified.
  2. Calculate the difference between next Monday and today with (7 - clonedDate.getDay()) % 7.
  3. Since getDay() return a number where 0 represent Sunday, we will + 1 to the result.
  4. Perform a % 7 to get the remainder.
  5. Sum the day-value to clonedDate object.

Below shows the examp

// Calculate number of days and add to date 
const retrieveMondayDate = (date = new Date()) => {
  const clonedDate = new Date(date.getTime());
  
  const numOfDays = ((7 - clonedDate.getDay()) % 7 + 1) % 7;
  const newDate = clonedDate.getDate() + numOfDays;

  return clonedDate.setDate(newDate);
};

// Use current date without passing in
let nextMonday = new Date(retrieveMondayDate());
console.log(nextMonday.toLocaleString());        // "09/05/2022, 13:10:34"

// Pass in current date
nextMonday = new Date(retrieveMondayDate(new Date()));
console.log(nextMonday.toLocaleString());        // "09/05/2022, 13:10:34"

// Pass in other date
const xday = new Date('2022-03-30');
nextMonday = new Date(retrieveMondayDate(xday));
console.log(nextMonday.toLocaleString());        // "04/04/2022, 08:00:00"

2. Array Search Method

We will utilise an array with 7 fields where each fields will contain a count indicating number of days to Monday. Then we will find out the days by using the getDay() method to retrieve the day of the week which is also the index of the array.

Process Flow:

  1. Clone the date so the original date will not get modified.
  2. Define any array [1,7,6,5,4,3,2] representing the difference in number of days till next Monday. The array index is represented such that 0 is Sunday, 1 is Monday etc.
  3. Get the number of days with days[clonedDate.getDay()] since getDay() will return the day of the week; return the index position in array.
  4. Sum the day-value retrieved with the clonedDate object.
// Referencing with array to know the count till next Monday
const retrieveMondayDate = (date = new Date()) => {
  const clonedDate = new Date(date.getTime());
  
  const tillNextMonday = [1,7,6,5,4,3,2];
  const newDate = clonedDate.getDate() + tillNextMonday[clonedDate.getDay()];

  return clonedDate.setDate(newDate);
};


// Use current date without passing in
let nextMonday = new Date(retrieveMondayDate());
console.log(nextMonday.toLocaleString());        // "09/05/2022, 13:10:34"

// Pass in current date
nextMonday = new Date(retrieveMondayDate(new Date()));
console.log(nextMonday.toLocaleString());        // "09/05/2022, 13:10:34"

// Pass in other date
const xday = new Date('2022-03-30');
nextMonday = new Date(retrieveMondayDate(xday));
console.log(nextMonday.toLocaleString());        // "04/04/2022, 08:00:00"

Conclusion

We have look into 2 different methods to get date of next Monday in JavaScript.

Next Monday Calculation Method may be quite difficult to understand for some of us. Whereas array Search Method is much more easy to understand and we can easily change the array values to reflect the count to other day of the week.

Show Comments

No Responses Yet

Leave a Reply