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.

Walk civil days, skip weekendsDividing by 7 ignores partial weeks and holidaysWalk civil days, skip weekends(days/7)*5 estimatewrong for partial weeksiterate, count dayOfWeek 1-5exactCount each weekday in the range; subtract a holiday set if needed.

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;
}

Count weekdaysIncrement when dayOfWeek is 1-5Count weekdaysstart..enddayOfWeek<= 5?count

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;
}

With holidaysSkip weekends and a holiday setWith holidaysrange+holidaysweekday?not holiday?count

Verification snippet

Business Days Between assertionsKey cases assert correctlyAssertions that prove the edge caseMon-Fri week5includes weekendweekend skippedholiday in setnot countedstart==end0

Common pitfalls

Business Days Between pitfallsCommon mistakes and their fixesWrongRightdays between / 7 * 5ignores partial weeksiterate PlainDatecalendar-correctuse Date + 24h loopDST driftcheck dayOfWeek + holidaysexact

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.