Calculate Business Days Between Two Dates in JavaScript
To count business days, iterate calendar days with Temporal.PlainDate and count those whose dayOfWeek is 1-5, optionally skipping a holiday set. Part of Temporal.Duration Arithmetic.
Why this scenario is tricky
There is no closed-form business-day count that survives arbitrary holidays, so the reliable approach is to walk the range day by day on a calendar type. Doing it on Date risks DST drift; doing it on PlainDate means each step is exactly one civil day and dayOfWeek (1 = Monday … 7 = Sunday) is unambiguous.
Minimal working solution
import { Temporal } from '@js-temporal/polyfill';
function businessDays(start: Temporal.PlainDate, end: Temporal.PlainDate) {
let n = 0;
for (let d = start; Temporal.PlainDate.compare(d, end) < 0; d = d.add({ days: 1 }))
if (d.dayOfWeek <= 5) n++; // 1..5 = Mon..Fri
return n;
}
Full production version
import { Temporal } from '@js-temporal/polyfill';
function businessDays(start: Temporal.PlainDate, end: Temporal.PlainDate, holidays = new Set<string>()) {
let n = 0;
for (let d = start; Temporal.PlainDate.compare(d, end) < 0; d = d.add({ days: 1 }))
if (d.dayOfWeek <= 5 && !holidays.has(d.toString())) n++;
return n;
}
Verification snippet
Common pitfalls
Frequently Asked Questions
How do I count business days between two dates?
Iterate the range on Temporal.PlainDate and count the days whose dayOfWeek is 1 through 5 (Monday to Friday). To exclude public holidays, keep a Set of their ISO strings and skip any date it contains.
Why not just divide the day count by seven and multiply by five?
That estimate is wrong whenever the range covers partial weeks or contains holidays. Because Temporal.PlainDate.dayOfWeek is exact and holidays are arbitrary, walking the range and counting is the only reliable method.