Get the Date of the Next Monday or Friday in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
4 min

banner

# Table of Contents

  1. Get the Date of the Next Monday using JavaScript
  2. Get the Date of the Next Friday using JavaScript

# Get the Date of the Next Monday using JavaScript

To get the date of the next Monday:

  1. Get the day of the month of the next Monday.
  2. Pass the result to the setDate() method.
  3. The setDate method changes the day of the month for the given date.
index.js
function getNextMonday(date = new Date()) { const dateCopy = new Date(date.getTime()); const nextMonday = new Date( dateCopy.setDate( dateCopy.getDate() + ((7 - dateCopy.getDay() + 1) % 7 || 7), ), ); return nextMonday; } // ๐Ÿ‘‡๏ธ Get Monday of Next Week console.log(getNextMonday(new Date())); // ๐Ÿ‘‰๏ธ Mon Jul 31 2023 // ๐Ÿ‘‡๏ธ Get Next Monday for specific Date console.log(getNextMonday(new Date('2022-01-25'))); // ๐Ÿ‘‰๏ธ Mon Jan 31 2022

get the date of the next monday

The code for this article is available on GitHub

We created a reusable function that takes a Date object as a parameter and returns the next Monday.

If no parameter is provided, the function returns the next Monday of the current date.

The Date.setDate() method allows us to change the day of the month of a specific Date instance.

The method takes an integer that represents the day of the month.

To get the next Monday, we:

  1. Add 1 to the day of the week, e.g. Tuesday = 7 - 2 (Tuesday) + 1 = 6. Note that the Date.getDay() method returns the day of the week where Sunday is 0, Monday is 1, Tuesday is 2, etc.

  2. Use the modulo operator to get the remainder of dividing 6 % 7 = 6

  3. If the remainder is equal to 0, then the current date is Monday and we have to default it to 7 to get the date of the next Monday.

  4. The getDate() method returns the day of the month, e.g. 18 + 6 = 24, where 24 is the day of the month for the next Monday.

If the Date object stores a Monday:

  1. Get the day of the month, e.g. 17.
  2. Calculate 7 - day of the week (Monday = 1) + 1 = 7
  3. Get the remainder - 7 % 7 = 0 || 7 = 7.
  4. Get the day of the month for the next Monday - 17 + 7 = 24, where 24 is the day of the month for the next Monday.

We created the dateCopy variable in the function because the setDate method mutates the Date instance in place.

If you use the same Date object elsewhere in your code, this might lead to confusing bugs.

# Get the Date of the Next Friday using JavaScript

To get the date of the next Friday:

  1. Get the day of the month of the next Friday.
  2. Pass the result to the setDate() method.
  3. The setDate method changes the day of the month for the given date.
index.js
function getNextFriday(date = new Date()) { const dateCopy = new Date(date.getTime()); const nextFriday = new Date( dateCopy.setDate( dateCopy.getDate() + ((7 - dateCopy.getDay() + 5) % 7 || 7), ), ); return nextFriday; } // ๐Ÿ‘‡๏ธ Get Friday of Next Week console.log(getNextFriday(new Date())); // ๐Ÿ‘‰๏ธ Fri Aug 04 2023 // ๐Ÿ‘‡๏ธ Get Next Friday for specific Date console.log(getNextFriday(new Date('2022-01-25'))); // ๐Ÿ‘‰๏ธ Fri Jan 28 2022

get date of next friday using javascript

The code for this article is available on GitHub

We created a reusable function that takes a Date object as a parameter and returns the next Friday.

If no parameter is provided, the function returns the next Friday of the current date.

The Date.setDate() method allows us to change the day of the month of a specific Date instance.

The method takes an integer that represents the day of the month.

To get the next Friday, we:

  1. Add 5 to the day of the week, e.g. Tuesday = 7 - 2 (Tuesday) + 5 = 10. Note that the Date.getDay() method returns the day of the week where Sunday is 0, Monday is 1, Tuesday is 2, etc.

  2. Use the modulo operator to get the remainder of dividing 10 % 7 = 3

  3. If the remainder is equal to 0, then the current date is Friday and we have to default it to 7 to get the date of the next Friday.

  4. The getDate() method returns the day of the month, e.g. 18 + 3 = 21, where 21 is the day of the month for the next Friday.

If the Date object stores a Friday:

  1. Get the day of the month, e.g. 21.
  2. Calculate 7 - day of the week (Friday = 5) + 5 = 7
  3. Get the remainder - 7 % 7 = 0 || 7 = 7.
  4. Get the day of the month for the next Friday - 21 + 7 = 28, where 28 is the day of the month for the next Friday.

We created the dateCopy variable in the function because the setDate method mutates the Date instance in place.

If you use the same Date object elsewhere in your code, this might lead to confusing bugs.

The code for this article is available on GitHub

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev