Business Day Calculator — TypeScript source
Step N business days from a date or count the business days in a span — with your weekend definition (Sat-Sun, Fri-Sat, Sunday, none) and your holiday list. UTC dates, no DST traps; neighboring business days shown alongside.
This is the TypeScript implementation — the same logic the interactive tool runs, in a shareable, citable form.
// Pure surface of the business-day-calculator tool (slug:
// business-day-calculator). Dates are YYYY-MM-DD strings read as UTC midnights
// — no DST traps, deterministic for the Go twin. Weekend and holidays are
// INPUTS (defaults: Sat/Sun, none): callers wire their own holiday list; the
// island ships a small fixed set of common holidays as example data only.
export interface BusinessDayOptions {
/** UTC weekday numbers off work; 0=Sunday … 6=Saturday. Default [0, 6]. */
weekendDays?: number[];
/** Holiday dates, YYYY-MM-DD. */
holidays?: string[];
}
const DEFAULTS: Required<BusinessDayOptions> = { weekendDays: [0, 6], holidays: [] };
/** Parse YYYY-MM-DD to a UTC Date; returns null on anything else — including
* rollover dates like 2026-13-01 that Date.UTC would silently normalize. */
function parseUtcDate(iso: string): Date | null {
const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(iso);
if (!m) return null;
const y = Number(m[1]);
const mo = Number(m[2]);
const d = new Date(Date.UTC(y, mo - 1, Number(m[3])));
return d.getUTCFullYear() !== y || d.getUTCMonth() !== mo - 1 ? null : d;
}
const toIso = (d: Date): string => d.toISOString().slice(0, 10);
function isOff(day: Date, opts: Required<BusinessDayOptions>): boolean {
return opts.weekendDays.includes(day.getUTCDay()) || opts.holidays.includes(toIso(day));
}
/** True when the date is a real date AND a working day. */
export function isBusinessDay(iso: string, opts: BusinessDayOptions = {}): boolean {
const o = { ...DEFAULTS, ...opts };
const d = parseUtcDate(iso);
return d !== null && !isOff(d, o);
}
/** Step n working days from start (negative steps backward). n=0 rolls a
* non-business start onto the next business day in the step direction; n=1
* from a weekend is simply the following business day. Invalid → null. */
export function addBusinessDays(start: string, n: number, opts: BusinessDayOptions = {}): string | null {
const o = { ...DEFAULTS, ...opts };
const d = parseUtcDate(start);
if (!d) return null;
const step = n >= 0 ? 1 : -1;
const steps = Math.abs(Math.trunc(n));
let cur = d;
if (steps === 0) {
while (isOff(cur, o)) cur = new Date(cur.getTime() + step * 86400000);
return toIso(cur);
}
let counted = 0;
while (counted < steps) {
cur = new Date(cur.getTime() + step * 86400000);
if (!isOff(cur, o)) counted += 1;
}
return toIso(cur);
}
/** Count business days in [a, b) — half-open, chronological: if b precedes a
* the pair is swapped and counted over [b, a). Same-day input is 0. */
export function businessDaysBetween(a: string, b: string, opts: BusinessDayOptions = {}): number | null {
const o = { ...DEFAULTS, ...opts };
let da = parseUtcDate(a);
let db = parseUtcDate(b);
if (!da || !db) return null;
if (da > db) [da, db] = [db, da];
let count = 0;
for (let d = da; d < db; d = new Date(d.getTime() + 86400000)) {
if (!isOff(d, o)) count += 1;
}
return count;
}
/** Next working day strictly after the date. */
export function nextBusinessDay(iso: string, opts: BusinessDayOptions = {}): string | null {
const o = { ...DEFAULTS, ...opts };
const d = parseUtcDate(iso);
if (!d) return null;
let next = new Date(d.getTime() + 86400000);
while (isOff(next, o)) next = new Date(next.getTime() + 86400000);
return toIso(next);
}
/** Previous working day strictly before the date. */
export function previousBusinessDay(iso: string, opts: BusinessDayOptions = {}): string | null {
const o = { ...DEFAULTS, ...opts };
const d = parseUtcDate(iso);
if (!d) return null;
let prev = new Date(d.getTime() - 86400000);
while (isOff(prev, o)) prev = new Date(prev.getTime() - 86400000);
return toIso(prev);
}
Also available in 1 other language
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in several languages — the same contract, ported. Compare all languages side by side →