Add Business Days Skipping Weekends

Move a UTC date forward or backward by business days while skipping weekends and an optional set of holiday date keys.

TypescriptData Integrity

Code

typescriptsnippet
37 lines
export function addBusinessDays(
  start: Date,
  days: number,
  holidays: ReadonlySet<string> = new Set(),
): Date {
  if (!(start instanceof Date) || Number.isNaN(start.getTime())) {
    throw new TypeError("start must be a valid Date");
  }
  if (!Number.isInteger(days)) throw new TypeError("days must be an integer");
  const result = new Date(start.getTime());
  let remaining = Math.abs(days);
  const step = days < 0 ? -1 : 1;
  while (remaining > 0) {
    result.setUTCDate(result.getUTCDate() + step);
    if (isBusinessDay(result, holidays)) remaining -= 1;
  }
  return result;
}

export function isBusinessDay(date: Date, holidays: ReadonlySet<string> = new Set()): boolean {
  if (!(date instanceof Date) || Number.isNaN(date.getTime())) {
    throw new TypeError("date must be a valid Date");
  }
  const weekday = date.getUTCDay();
  if (weekday === 0 || weekday === 6) return false;
  return !holidays.has(toDateKey(date));
}

export function toDateKey(date: Date): string {
  return date.toISOString().slice(0, 10);
}

const holidays = new Set(["2026-12-25"]);
console.log(addBusinessDays(new Date("2026-12-24T12:00:00Z"), 1, holidays).toISOString());
console.log(addBusinessDays(new Date("2026-12-28T00:00:00Z"), -1, holidays).toISOString());
console.log(isBusinessDay(new Date("2026-10-24T00:00:00Z")));
console.log(addBusinessDays(new Date("2026-10-21T00:00:00Z"), 0).toISOString());

Output

Captured from a sandboxed run of this snippet in the build pipeline.

2026-12-28T12:00:00.000Z
2026-12-24T00:00:00.000Z
false
2026-10-21T00:00:00.000Z

How it works

Add Business Days Skipping Weekends keeps everything in the functions addBusinessDays, isBusinessDay and toDateKey.

Repetition is confined to while (remaining > 0) — the rest of the file is straight-line. Invalid states are rejected up front: it throws TypeError instead of continuing with bad data.

What to watch for

  • The guard weekday === 0 || weekday === 6 short-circuits the flow with an early return when its condition holds.

Putting it to use

The main signature reads export function addBusinessDays( start: Date, days: number, holidays: ReadonlySet<string> = new Set()

addBusinessDays is the public entry point; everything else in the file exists to support it.

Core lines

if (!Number.isInteger(days)) throw new TypeError("days must be an integer");
throw new TypeError("start must be a valid Date");
throw new TypeError("date must be a valid Date");

Interactive Tools for This Topic

FAQ

How does the code behave when input is bad?
Validation comes first — `TypeError` marks the failure at `weekday === 0 || weekday === 6`, before any partial work happens.
Anything to download besides the language toolchain?
Nothing beyond the toolchain you already have for this language. The file defines `addBusinessDays`, `isBusinessDay`, `toDateKey` at load time.