Find the nth weekday of a month in JavaScript
Advanced · 4 min read
Many dates in real life are written as a rule instead of a number. "The team meets on the first Monday of every month." "Payday is the last Friday." In the United States, Thanksgiving is the fourth Thursday of November. To put these on a calendar, you need to turn the rule into an actual date.
In this tutorial you will write nthWeekdayOfMonth(year, month, weekday, n), which finds the third Thursday, the first Monday, and so on. It returns null when the date doesn't exist, like a fifth Monday in a month that only has four. Then you will write lastWeekdayOfMonth, which works backwards from the end of the month. You need to know getDay() (0 for Sunday to 6 for Saturday), that months count from 0, and the % remainder operator.
The idea: find the first one, then add weeks
Every weekday repeats every 7 days. So once you know the date of the first Thursday in a month, the second is 7 days later, the third is 14 days later, and so on:
- first Thursday: day
first - second:
first + 7 - third:
first + 14 - the nth:
first + (n - 1) * 7
The only real work is finding that first one.
The weekday gap
If the 1st of the month is weekday a and you want weekday w, the number of days to move forward is (w - a + 7) % 7. The + 7 keeps the answer from going negative, because in JavaScript -3 % 7 is -3. The result is always between 0 and 6.
Here is the exercise. Try it on your own first if you like, then read on and we will build it together.
49 The third Thursday of the month
hardReturn the nth occurrence of a weekday in a month — the third Thursday, the first Monday — and null when there is no such date. Then the last one, which needs the opposite approach.
Stuck? Three hints, smallest first
- 1. Find the first matching weekday in the month, then add whole weeks.
- 2. The fifth of a weekday often does not exist, so check against the month length and return
null. - 3. For the last one, start from the last day of the month and step backwards.
Step 1: helpers
const iso = (d) => d.getFullYear() + '-' +
String(d.getMonth() + 1).padStart(2, '0') + '-' +
String(d.getDate()).padStart(2, '0');
const daysInMonth = (year, month) => new Date(year, month + 1, 0).getDate();
console.log(daysInMonth(2026, 0)); // 31
console.log(new Date(2026, 0, 1).getDay()); // 4
iso shows a date as YYYY-MM-DD. daysInMonth uses a handy trick: day 0 of the next month is the last day of this one. 1 January 2026 is a Thursday, weekday 4.
Step 2: the first match in the month
const firstMatch = (year, month, weekday) =>
1 + ((weekday - new Date(year, month, 1).getDay() + 7) % 7);
console.log(firstMatch(2026, 0, 4)); // 1
console.log(firstMatch(2026, 0, 1)); // 5
The first Thursday of January 2026 is the 1st itself, a gap of 0. The first Monday is 4 days later, on the 5th. The 1 + is there because the month starts on day 1, not day 0.
Step 3: add whole weeks
const third = firstMatch(2026, 0, 4) + (3 - 1) * 7;
console.log(third); // 15
The third Thursday is 1 + 14 = the 15th.
Step 4: the wrong turn, a fifth that doesn't exist
January 2026 has only four Mondays. Ask for the fifth anyway:
const fifthMonday = firstMatch(2026, 0, 1) + (5 - 1) * 7;
console.log(fifthMonday); // 33
console.log(iso(new Date(2026, 0, fifthMonday))); // '2026-02-02'
Day 33 doesn't exist, but new Date doesn't complain. It rolls over into February and gives 2 February. A meeting would quietly land in the wrong month.
So check the day against the length of the month, and return null when it doesn't fit:
function nthWeekdayOfMonth(year, month, weekday, n) {
const firstOfMonth = new Date(year, month, 1);
const first = 1 + ((weekday - firstOfMonth.getDay() + 7) % 7);
const day = first + (n - 1) * 7;
if (day > daysInMonth(year, month)) return null;
return new Date(year, month, day);
}
console.log(iso(nthWeekdayOfMonth(2026, 0, 4, 3))); // '2026-01-15'
console.log(iso(nthWeekdayOfMonth(2026, 0, 4, 5))); // '2026-01-29'
console.log(nthWeekdayOfMonth(2026, 0, 1, 5)); // null
console.log(iso(nthWeekdayOfMonth(2026, 10, 4, 4))); // '2026-11-26'
January 2026 does have a fifth Thursday, the 29th, but no fifth Monday. The last line is the fourth Thursday of November 2026, which is the 26th.
null is the honest answer. Picking the fourth Monday instead would hide the problem and put the event in a different week.
Step 5: the last one, counting backwards
"The last Friday" isn't the same as "the fifth Friday", because some months have only four. Instead, start at the last day and step back to the weekday you want:
function lastWeekdayOfMonth(year, month, weekday) {
const last = new Date(year, month + 1, 0);
last.setDate(last.getDate() - ((last.getDay() - weekday + 7) % 7));
return last;
}
console.log(iso(lastWeekdayOfMonth(2026, 0, 5))); // '2026-01-30'
console.log(iso(lastWeekdayOfMonth(2026, 1, 6))); // '2026-02-28'
31 January 2026 is a Saturday (6). The gap back to Friday (5) is (6 - 5 + 7) % 7, which is 1, so the last Friday is the 30th. In February 2026, the 28th is itself a Saturday, so the gap is 0.
This direction never needs a null. Every weekday appears at least four times in every month, so there is always a last one. Notice February's last Saturday is only its fourth Saturday.
The full solution
The panel has nthWeekdayOfMonth and lastWeekdayOfMonth. It prints the third Thursday, the first Monday, the missing fifth Monday and the last Friday of January 2026. The checks cover Thanksgiving 2026, February's last Saturday and that every answer 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 daysInMonth = (year, month) => new Date(year, month + 1, 0).getDate();
function nthWeekdayOfMonth(year, month, weekday, n) {
const firstOfMonth = new Date(year, month, 1);
const firstMatch = 1 + ((weekday - firstOfMonth.getDay() + 7) % 7);
const day = firstMatch + (n - 1) * 7;
if (day > daysInMonth(year, month)) return null; // there is no fifth one
return new Date(year, month, day);
}
function lastWeekdayOfMonth(year, month, weekday) {
const last = new Date(year, month + 1, 0); // the last day of the month
last.setDate(last.getDate() - ((last.getDay() - weekday + 7) % 7));
return last;
}
console.log('third Thursday of January 2026:', iso(nthWeekdayOfMonth(2026, 0, 4, 3)));
console.log('first Monday of January 2026:', iso(nthWeekdayOfMonth(2026, 0, 1, 1)));
console.log('fifth Monday of January 2026:', nthWeekdayOfMonth(2026, 0, 1, 5), '<- there is not one');
console.log('last Friday of January 2026:', iso(lastWeekdayOfMonth(2026, 0, 5)));
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 third Thursday of January 2026', iso(nthWeekdayOfMonth(2026, 0, 4, 3)), '2026-01-15');
test('the first Monday', iso(nthWeekdayOfMonth(2026, 0, 1, 1)), '2026-01-05');
test('the fifth Thursday exists in January 2026', iso(nthWeekdayOfMonth(2026, 0, 4, 5)), '2026-01-29');
test('the fifth Monday does not', nthWeekdayOfMonth(2026, 0, 1, 5), null);
test('the last Friday of January 2026', iso(lastWeekdayOfMonth(2026, 0, 5)), '2026-01-30');
test('the fourth Thursday of November 2026, for the American holiday', iso(nthWeekdayOfMonth(2026, 10, 4, 4)), '2026-11-26');
test('the last Saturday of February 2026 is also the last day of it', iso(lastWeekdayOfMonth(2026, 1, 6)), '2026-02-28');
test('the answer always has the weekday asked for', nthWeekdayOfMonth(2026, 0, 4, 3).getDay(), 4);
test('and so does the last-of-month version', lastWeekdayOfMonth(2026, 0, 5).getDay(), 5);
Recurring rules are written this way — "the second Tuesday of every month", "the last Friday of the quarter" — and the two directions need different code. Counting forwards from the first of the month can overrun, so it has to be able to say null; counting back from the last day cannot overrun, because every weekday occurs at least four times in every month.
Returning null rather than clamping to the fourth is the honest choice: "the fifth Monday of January 2026" has no answer, and quietly substituting a different date would put a meeting in the wrong week. Note the "last" form is not the same as the fifth: February 2026's last Saturday is its fourth.
Common mistakes
- Not checking the month length.
new Daterolls day 33 into the next month instead of failing. Compare withdaysInMonthand returnnull. - Treating "last" as "fifth". Many months have only four of a weekday. Count back from the last day instead.
- Forgetting months count from 0. November is
10, not11.
Try it yourself
- List the second Tuesday of every month in 2026.
- Find every month in 2026 that has five Fridays.
- Write
isLastWeekdayOfMonth(date)that istruewhen there is no same weekday later in that month.