Find the next Monday after a date in JavaScript
Advanced · 4 min read
"The class starts next Monday." "Payday is the next Friday." "The bin is collected every Thursday." Finding the next date that falls on a given weekday is a building block for schedules, reminders and booking forms.
In this tutorial you will write nextWeekday(from, weekday), which finds the next Monday (or any weekday) strictly after a date. Then you will write weekdayOnOrAfter(from, weekday), which also accepts the date itself when it already matches. You need to know getDay(), which gives 0 for Sunday up to 6 for Saturday, and the % remainder operator.
Weekdays go round in a circle
After Saturday (6) comes Sunday (0) again. Counting that wraps around like this is called modular arithmetic, and % is how you do it. A clock face is the everyday example: 3 hours after 11 o'clock is 2 o'clock, not 14.
To find how many days to go from today's weekday to the one we want, we subtract and wrap the answer into the range 0 to 6.
A surprise with negative numbers
In JavaScript, % keeps the sign of the number on the left:
console.log(10 % 7); // 3
console.log(-3 % 7); // -3
On the week circle, 3 days back from Sunday (0) is Thursday (4), but JavaScript says -3. To keep the answer positive, add 7 before taking the remainder: (-3 + 7) % 7 is 4. Adding 7 never changes the weekday, because 7 days later is the same weekday.
Here is the exercise. Try it on your own first if you like, then read on and we will build it step by step.
48 The next Monday
hardFind the next occurrence of a given weekday after a date. Then write the variant that returns the date itself when it already is that weekday.
Stuck? Three hints, smallest first
- 1. The gap is
(wanted - current + 7) % 7, which lands in 0 to 6. - 2. A gap of 0 means the date already is that weekday, so "next" has to jump a whole week.
- 3.
|| 7turns that 0 into 7, which is exactly the rule you want.
Step 1: helpers
const iso = (d) => d.getFullYear() + '-' +
String(d.getMonth() + 1).padStart(2, '0') + '-' +
String(d.getDate()).padStart(2, '0');
const justTheDate = (date) => new Date(date.getFullYear(), date.getMonth(), date.getDate());
const thursday = new Date(2026, 0, 15);
console.log(thursday.getDay()); // 4
iso shows a date as YYYY-MM-DD. justTheDate makes a fresh copy at midnight, dropping any time of day. 15 January 2026 is a Thursday, weekday 4.
Step 2: the wrong turn, plain subtraction
Monday is weekday 1. The gap from Thursday seems to be 1 - 4:
const wrong = justTheDate(thursday);
wrong.setDate(wrong.getDate() + (1 - 4));
console.log(iso(wrong)); // '2026-01-12'
That moved back three days to the Monday before. We wanted the Monday after.
Step 3: wrap the gap into 0 to 6
const gapTo = (weekday, from) => (weekday - from.getDay() + 7) % 7;
console.log(gapTo(1, thursday)); // 4
console.log(gapTo(5, thursday)); // 1
console.log(gapTo(4, thursday)); // 0
From Thursday, Monday is 4 days ahead and Friday is 1 day ahead. But Thursday itself gives 0, because the date already is a Thursday.
Step 4: "next" means skip today
For "the next Thursday", a gap of 0 is wrong: we want a full week on. JavaScript's || helps here. a || b gives a unless a is a "falsy" value such as 0, in which case it gives b:
console.log(4 || 7); // 4
console.log(0 || 7); // 7
So gap || 7 keeps every gap from 1 to 6 and turns only 0 into 7. That is exactly the rule.
function nextWeekday(from, weekday) {
const copy = justTheDate(from);
const gap = (weekday - copy.getDay() + 7) % 7 || 7;
copy.setDate(copy.getDate() + gap);
return copy;
}
console.log(iso(nextWeekday(thursday, 1))); // '2026-01-19'
console.log(iso(nextWeekday(thursday, 5))); // '2026-01-16'
console.log(iso(nextWeekday(thursday, 4))); // '2026-01-22'
Next Monday is the 19th, next Friday is tomorrow, the 16th, and next Thursday is a week on, the 22nd.
Step 5: "on or after" keeps today
Sometimes you want today if it matches, for example "the first Monday on or after the start date". Drop the || 7:
function weekdayOnOrAfter(from, weekday) {
const copy = justTheDate(from);
copy.setDate(copy.getDate() + ((weekday - copy.getDay() + 7) % 7));
return copy;
}
console.log(iso(weekdayOnOrAfter(thursday, 4))); // '2026-01-15'
These are two different questions, so they are two functions. A single function with a true/false flag, like nextWeekday(d, 1, true), leaves the reader guessing which question is being asked.
Step 6: months and years take care of themselves
setDate rolls over month and year ends for you:
console.log(iso(nextWeekday(new Date(2026, 0, 30), 1))); // '2026-02-02'
console.log(iso(nextWeekday(new Date(2026, 11, 31), 5))); // '2027-01-01'
console.log(iso(nextWeekday(new Date(2026, 0, 18), 0))); // '2026-01-25'
The Monday after Friday 30 January is 2 February. The Friday after Thursday 31 December 2026 is New Year's Day 2027. From a Sunday, the next Sunday is seven days on.
The full solution
The panel has both functions. It prints the next Monday, Friday and Thursday from 15 January, and the "on or after" Thursday. The checks cover a gap of nearly a week, the full-week jump, month and year ends, and that the answer always has the weekday you asked for. Press Run to see them.
const iso = (d) => d.getFullYear() + '-' +
String(d.getMonth() + 1).padStart(2, '0') + '-' +
String(d.getDate()).padStart(2, '0');
const justTheDate = (date) => new Date(date.getFullYear(), date.getMonth(), date.getDate());
// Strictly after: today never counts, so a matching weekday jumps a week.
function nextWeekday(from, weekday) {
const copy = justTheDate(from);
const gap = (weekday - copy.getDay() + 7) % 7 || 7; // 0 becomes 7
copy.setDate(copy.getDate() + gap);
return copy;
}
// On or after: today counts if it matches.
function weekdayOnOrAfter(from, weekday) {
const copy = justTheDate(from);
copy.setDate(copy.getDate() + ((weekday - copy.getDay() + 7) % 7));
return copy;
}
const thursday = new Date(2026, 0, 15); // 15 January 2026
console.log('from Thu 15/01, next Monday :', iso(nextWeekday(thursday, 1)));
console.log('from Thu 15/01, next Friday :', iso(nextWeekday(thursday, 5)));
console.log('from Thu 15/01, next Thursday:', iso(nextWeekday(thursday, 4)), '<- a week on');
console.log('from Thu 15/01, Thursday on or after:', iso(weekdayOnOrAfter(thursday, 4)), '<- today');
Output
9 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 9 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('the next Monday', iso(nextWeekday(thursday, 1)), '2026-01-19');
test('the next Friday is tomorrow', iso(nextWeekday(thursday, 5)), '2026-01-16');
test('the next Wednesday is nearly a week away', iso(nextWeekday(thursday, 3)), '2026-01-21');
test('the next Thursday is a full week on, not today', iso(nextWeekday(thursday, 4)), '2026-01-22');
test('whereas "on or after" returns today', iso(weekdayOnOrAfter(thursday, 4)), '2026-01-15');
test('the answer always has the weekday asked for', nextWeekday(thursday, 1).getDay(), 1);
test('it crosses a month end without help', iso(nextWeekday(new Date(2026, 0, 30), 1)), '2026-02-02');
test('and a year end', iso(nextWeekday(new Date(2026, 11, 31), 5)), '2027-01-01');
test('from a Sunday, the next Sunday is seven days on', iso(nextWeekday(new Date(2026, 0, 18), 0)), '2026-01-25');
(wanted - current + 7) % 7 is the same modular step as the start of the week in exercise 34, run forwards. The + 7 keeps it out of negative territory, because -3 % 7 is -3 in JavaScript, not 4.
The || 7 is the interesting line, and it is only correct because the value it is guarding is 0. Two functions rather than a boolean flag is the better shape here: "next Monday" and "Monday on or after today" are different questions, and a caller reading nextWeekday(d, 1, true) has no idea which one it is asking.
Common mistakes
- Forgetting the
+ 7.-3 % 7is-3in JavaScript, so the date moves backwards. - Returning today for "next". A gap of 0 means the date already matches. Use
|| 7to jump a week. - Using
|| 7on other values. It only works here because the one value it replaces is0. Don't copy it to code where 0 is a valid answer.
Try it yourself
- Write
previousWeekday(from, weekday)that finds the last Monday strictly before a date. - List the next four Fridays after 15 January 2026.
- Write
daysUntil(from, weekday)that returns just the number of days to the next one.