UNPKG

@northguild/gmt

Version:

Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.

43 lines (41 loc) • 2.57 kB
import { stepBusinessDaysValue } from "../../internal/businessDayValues.js"; // src/plain/calculate/subtractBusinessDays.ts /** * Return a PlainDate ISO string with `amount` working days subtracted from `value`. * * - `calendar` names the weekend days and holidays to skip. Omit it and GMT keeps its original * fixed Monday–Friday week with no holidays. Pass one anywhere the weekend is not * Saturday–Sunday — much of the Middle East is Friday–Saturday. * - Negative `amount` behaves identically to `addBusinessDays(value, Math.abs(amount), cal)`. * - `amount` of 0 returns `value` unchanged — including when `value` is not itself a working * day. Use `rollDate` to move a non-working day onto one. * - `value` is never counted, so subtracting 1 from a holiday lands on the working day before * it. * - `value` and `calendar.holidays` are local dates; `calendar.timeZone` is not read. A caller * holding an instant reduces it to a local date first, with `floorToZone`. * - Returns "" on invalid input — including a fractional `amount`, which names no whole * number of working days — on an invalid `calendar`, and when the walk runs past * 200,000 calendar days (about 547 years) without finishing. * * @param value ISO PlainDate string * @param amount whole number of working days to subtract; negative walks forwards * @param calendar optional BusinessCalendar; defaults to Monday–Friday with no holidays * @returns ISO PlainDate string after subtracting working days, or "" on invalid input * * @example subtractBusinessDays("2024-03-18", 1) // "2024-03-15" (Monday back to Friday) * @example subtractBusinessDays("2024-03-17", 1) // "2024-03-15" (from a Sunday) * @example subtractBusinessDays("2024-03-18", 0) // "2024-03-18" * @example subtractBusinessDays("2024-07-05", 1, { weekend: [6, 7], holidays: ["2024-07-04"], timeZone: "Africa/Kigali" }) // "2024-07-03" (skips the holiday) * @example subtractBusinessDays("2024-07-05", -1, { weekend: [6, 7], holidays: [], timeZone: "Asia/Tokyo" }) // "2024-07-08" * @example subtractBusinessDays("2024-07-07", 1, { weekend: [5, 6], holidays: [], timeZone: "Asia/Riyadh" }) // "2024-07-04" (Friday–Saturday weekend) * @example subtractBusinessDays("invalid", 1) // "" * @example subtractBusinessDays("2024-03-18", 1, { weekend: [6, 7], holidays: [], timeZone: "Invalid/Zone" }) // "" (bad calendar) */ function subtractBusinessDays(value, amount, calendar) { try { return stepBusinessDaysValue(value, amount, -1, calendar); } catch { return ""; } } export { subtractBusinessDays };