Count working days between two dates in JavaScript
Intermediate · 4 min read
"Delivery in 5 working days." "Payment within 10 business days." Many deadlines skip weekends and public holidays. Payroll, leave planners and delivery estimates all need to count only the days people actually work.
In this tutorial you will write workdays(from, to, holidays), which counts the working days between two dates, including both ends. It skips Saturdays, Sundays and a list of holidays. You'll also see why a holiday that falls on a Saturday doesn't cost an extra day. You need to know loops, if and how to create a date. Months count from 0, so January is 0.
The plan: walk one day at a time
The simplest reliable method is to start at the first date and step forward one day at a time until you pass the last date. For each day, ask: is it a weekend? Is it a holiday? If neither, count it.
A month has at most 31 days, so even a year is only 365 steps. That's no work at all for a computer.
Three tools we need
getDay() tells you the day of the week, with 0 for Sunday and 6 for Saturday:
console.log(new Date(2026, 0, 10).getDay()); // 6: Saturday
console.log(new Date(2026, 0, 12).getDay()); // 1: Monday
setDate(getDate() + 1) moves a date forward one day. It handles month and year ends for you, because a day past the end of the month rolls into the next one:
const d = new Date(2026, 0, 31);
d.setDate(d.getDate() + 1);
console.log(d.getMonth(), d.getDate()); // 1 1: 1 February
A Set is a collection you can ask "do you contain this?" with .has(). We'll store holidays as yyyy-mm-dd strings:
const HOLIDAYS = new Set(['2026-01-01', '2026-01-10']);
console.log(HOLIDAYS.has('2026-01-01')); // true
console.log(HOLIDAYS.has('2026-01-02')); // false
Here is the exercise. Try it on your own first if you like, or read on and we will build it together.
130 Working days between two dates
fairCount the working days from one date to another inclusive, skipping Saturdays, Sundays and a list of public holidays. Show that a holiday landing on a Saturday costs nothing.
Stuck? Three hints, smallest first
- 1.
getDay()returns 0 for Sunday and 6 for Saturday. - 2. Walk day by day with a copy of the start date, so the caller’s date is not mutated.
- 3. Hold the holidays as a Set of
yyyy-mm-ddkeys and look each day up.
Step 1: a key for each day
To look a date up in the Set, turn it into the same yyyy-mm-dd text:
const iso = (d) =>
d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0');
console.log(iso(new Date(2026, 0, 1))); // 2026-01-01
The + 1 turns the 0-based month back into the usual number, and padStart(2, '0') adds the leading zero.
Step 2: count weekdays
function workdays(from, to) {
const day = new Date(from.getFullYear(), from.getMonth(), from.getDate()); // a copy
const last = new Date(to.getFullYear(), to.getMonth(), to.getDate());
let count = 0;
while (day <= last) {
const dow = day.getDay();
if (dow !== 0 && dow !== 6) count++;
day.setDate(day.getDate() + 1);
}
return count;
}
console.log(workdays(new Date(2026, 0, 5), new Date(2026, 0, 9))); // 5: Monday to Friday
console.log(workdays(new Date(2026, 0, 3), new Date(2026, 0, 4))); // 0: a weekend
console.log(workdays(new Date(2026, 0, 1), new Date(2026, 0, 31))); // 22
day <= last includes the last date, so Monday to Friday is 5, not 4. When someone says "from Monday to Friday" they mean both ends.
We make a copy of from before the loop. setDate changes the date it's called on, and if we used from directly the caller's date would end up a day past to. Building the copy from year, month and day also drops any time of day, so the comparison with last is fair.
Step 3: skip the holidays
Now add the holiday check, after the weekend check:
const iso = (d) =>
d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0');
const HOLIDAYS = new Set(['2026-01-01', '2026-01-10']);
function workdays(from, to, holidays) {
const day = new Date(from.getFullYear(), from.getMonth(), from.getDate());
const last = new Date(to.getFullYear(), to.getMonth(), to.getDate());
let count = 0;
const skipped = [];
while (day <= last) {
const dow = day.getDay();
if (dow === 0 || dow === 6) skipped.push('weekend');
else if (holidays.has(iso(day))) skipped.push(iso(day));
else count++;
day.setDate(day.getDate() + 1);
}
return { count, holidaysTaken: skipped.filter((s) => s !== 'weekend') };
}
const january = workdays(new Date(2026, 0, 1), new Date(2026, 0, 31), HOLIDAYS);
console.log(january.count); // 21
console.log(january.holidaysTaken); // [ '2026-01-01' ]
Two holidays are on the list, but January drops from 22 working days to only 21. The skipped list shows why: only 1 January was taken as a holiday.
Step 4: a holiday on a Saturday
10 January 2026 is a Saturday. The weekend check runs first, so that day is already skipped as a weekend and the holiday check never sees it. A holiday on a weekend costs nothing, which matches real life unless a Monday off is declared instead. (You'd add that Monday to the holiday list.)
The order of the if and else if is what makes this work. If the holiday check came first, the count would still be 21, but holidaysTaken would wrongly list 10 January as a lost working day.
The full solution
The answer panel has iso, the holiday Set and workdays, and prints January 2026's working days and the holidays that cost a day. The checks cover a single week, a weekend, a single day, a fortnight across a month boundary, and that the caller's start date doesn't move. Press Run to see the output.
const iso = (d) =>
d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0');
// Two of these fall on a weekday, one on a Saturday.
const HOLIDAYS = new Set(['2026-01-01', '2026-01-10']);
function workdays(from, to, holidays) {
const day = new Date(from.getFullYear(), from.getMonth(), from.getDate()); // a copy
const last = new Date(to.getFullYear(), to.getMonth(), to.getDate());
let count = 0;
const skipped = [];
while (day <= last) {
const dow = day.getDay();
if (dow === 0 || dow === 6) skipped.push('weekend');
else if (holidays.has(iso(day))) skipped.push(iso(day));
else count++;
day.setDate(day.getDate() + 1); // rolls month and year on its own
}
return { count, holidaysTaken: skipped.filter((s) => s !== 'weekend') };
}
const january = workdays(new Date(2026, 0, 1), new Date(2026, 0, 31), HOLIDAYS);
console.log('January 2026:', january.count, 'working days; lost to holidays:', january.holidaysTaken);
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('January 2026 has 21 working days once the holidays are out', january.count, 21);
test('without any holiday list it is 22', workdays(new Date(2026, 0, 1), new Date(2026, 0, 31), new Set()).count, 22);
test('so only one of the two holidays cost anything', january.holidaysTaken, ['2026-01-01']);
test('because 10 January 2026 is a Saturday, already skipped', new Date(2026, 0, 10).getDay(), 6);
test('Monday to Friday is five, and both ends are counted', workdays(new Date(2026, 0, 5), new Date(2026, 0, 9), new Set()).count, 5);
test('a weekend on its own is none', workdays(new Date(2026, 0, 3), new Date(2026, 0, 4), new Set()).count, 0);
test('one weekday is one, so from and to may be the same date', workdays(new Date(2026, 0, 15), new Date(2026, 0, 15), new Set()).count, 1);
test('a fortnight is ten, and the walk crosses a month boundary happily', [workdays(new Date(2026, 0, 5), new Date(2026, 0, 16), new Set()).count, workdays(new Date(2026, 0, 26), new Date(2026, 1, 6), new Set()).count], [10, 10]);
test('and the caller\u2019s start date is not moved by the loop', (() => { const start = new Date(2026, 0, 5); workdays(start, new Date(2026, 0, 9), new Set()); return iso(start); })(), '2026-01-05');
Three decisions are worth naming out loud. The range is inclusive of both ends, which is what a human means by "from Monday to Friday" and not what slice or a naive difference gives; state it in the function's name or its documentation, because the off-by-one is invisible otherwise. The weekend is Saturday and Sunday, which is a cultural assumption, not a fact — it needs to be a parameter the moment the calendar covers a country where the week ends elsewhere. And holidays are checked only after the weekend, so a public holiday on a Saturday costs nothing, which is the truth in Nigeria unless the government declares the Monday in lieu.
The loop itself teaches the other half: day.setDate(day.getDate() + 1) steps across month ends and year ends with no special handling, because setDate takes an offset from the first of the month. And it mutates, which is why the function copies the incoming date before touching it — the last assertion is there because a helper that quietly moves its caller's Date is a bug that surfaces three functions away.
Common mistakes
- Leaving out the last day. Use
<=so "Monday to Friday" counts five days. - Stepping the caller's own date.
setDatemutates it. Copy it first. - Assuming Saturday and Sunday everywhere. Some countries have a different weekend. Make it a parameter if your users are spread out.
Try it yourself
- Add a
weekendparameter, an array like[0, 6], and count a week that ends on Friday and Saturday instead. - Write
addWorkdays(date, n)that returns the datenworking days later. - Count the working days in every month of 2026 and print them as a list.