How to Get the Date of the Next Monday (or any Day) in JavaScript
A common requirement in date-based applications is to find the date of the next occurrence of a specific day of the week. For example, you might need to calculate the next shipping day (e.g., the next Monday) or find the date of the next weekly meeting (e.g., the next Friday).
This guide will teach you how to create a single, powerful, and reusable function to solve this problem. You will learn the simple mathematical logic required and see how to apply the function to find any upcoming weekday.
The Core Logic: Calculating the Days to Add
The logic for finding the next occurrence of a specific day is a simple, step-by-step process:
- Get the Day Indexes: In JavaScript,
date.getDay()returns a number where 0 is Sunday, 1 is Monday, ..., 6 is Saturday. We need to know the index of our current day and our target day. - Calculate the Difference: Subtract the current day's index from the target day's index.
- Adjust for the "Next" Week: If the result is positive, that's how many days we need to add. If the result is zero or negative (meaning the day is today or has already passed this week), we need to add 7 days to that result to get to the next week's occurrence.
- Set the New Date: Add the calculated number of days to the current date.
An example of the logic in action:
- Today is Wednesday (
getDay()= 3). - Our target is the next Monday (target day index = 1).
- Difference:
1 - 3 = -2. - Since
-2is not positive, we adjust:-2 + 7 = 5. - We need to add 5 days to get to the next Monday.
The Reusable Function (Best Practice)
For clean and maintainable code, it's best to encapsulate this logic in a reusable function that takes a target day of the week as an argument.
/**
* Gets the date of the next occurrence of a specific day of the week.
* @param {Date} startDate The date to start searching from.
* @param {number} dayOfWeek The target day of the week (0=Sunday, 1=Monday, ...).
* @returns {Date} The date of the next occurrence of that day.
*/
function getNextDayOfWeek(startDate, dayOfWeek) {
// Clone the date so we don't mutate the original
const date = new Date(startDate.getTime());
const currentDay = date.getDay();
let daysToAdd = dayOfWeek - currentDay;
// If the day is today or has already passed this week, add 7 days
if (daysToAdd <= 0) {
daysToAdd += 7;
}
date.setDate(date.getDate() + daysToAdd);
return date;
}
Practical Examples
With our getNextDayOfWeek function, finding any upcoming day is now trivial.
How to Get the Date of the Next Monday
For example, we need to find the date of the upcoming Monday, relative to today.
So, we simply call our function with 1 as the target day (since Monday = 1).
const today = new Date();
const nextMonday = getNextDayOfWeek(today, 1); // 1 = Monday
console.log('Today is:', today.toDateString());
console.log('Next Monday is:', nextMonday.toDateString());
How to Get the Date of the Next Friday
For example, we need to find the date of the upcoming Friday, relative to a specific date.
So, we call our function with 5 as the target day (since Friday = 5).
const specificDate = new Date('2023-10-25'); // A Wednesday
const nextFriday = getNextDayOfWeek(specificDate, 5); // 5 = Friday
console.log('Starting from:', specificDate.toDateString());
console.log('The next Friday is:', nextFriday.toDateString()); // Output: "Fri Oct 27 2023"
Conclusion
Finding the date of the next occurrence of a weekday is a common problem that is best solved with a single, reusable function.
- The core logic involves comparing the current day's index with a target day's index and adding the correct number of days.
- The best practice is to create a reusable function that is parameterized to find any day of the week, rather than writing separate functions for each day.
- Always clone your input
Dateobject (e.g.,new Date(date.getTime())) before performing mutations to avoid unexpected side effects.
By using this modular and logical approach, you can handle these date calculations cleanly and reliably.