@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.
62 lines (60 loc) • 3.94 kB
JavaScript
import { plainDateOf } from "../../internal/inputChecks.js";
import { epochDayOfDate } from "../../internal/epochDay.js";
import { businessDaysWithin, parseBusinessCalendar } from "../../internal/businessCalendar.js";
// src/calendar/business/businessDaysBetween.ts
/**
* Count the working days between two local dates, **excluding `start` and including `end`**.
*
* - The convention is stated, not inferred: the range is `(start, end]`. Counting from Monday
* to Friday of the same week gives 4, not 5 — Monday itself is not counted.
* - That makes it the inverse of `addBusinessDays` when `n ≥ 0` or `start` is a business day:
* if `end` is `addBusinessDays(start, n, calendar)`, this returns `n`. From a weekend or
* holiday `start`, a negative `n` counts one closer to zero, because `start` itself is
* excluded and is not a business day.
* - `end` before `start` returns a negative count, so swapping the arguments negates the
* result. An empty range is always `0`, never `-0`, and `businessDaysBetween(d, d, calendar)`
* is 0 for any `d`.
* - The count is computed, not walked, so there is no range limit: whole weeks contribute one
* of each weekday and only the holidays are visited individually.
* - Both dates 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 `null` when either date is not a valid ISO PlainDate, or `calendar` is not a valid
* `BusinessCalendar`. Narrow it with `isValidBusinessCalendar` to tell a misconfigured
* calendar apart from a genuine 0.
*
* @param start ISO PlainDate string, excluded from the count
* @param end ISO PlainDate string, included in the count
* @param calendar BusinessCalendar naming the weekend days and holidays
* @returns number of working days in `(start, end]`, negative when `end` precedes `start`, or null on invalid input
*
* @example businessDaysBetween("2024-07-01", "2024-07-05", { weekend: [6, 7], holidays: ["2024-07-04"], timeZone: "Africa/Kigali" }) // 3 — Tue, Wed, Fri
* @example businessDaysBetween("2024-07-05", "2024-07-08", { weekend: [6, 7], holidays: [], timeZone: "UTC" }) // 1 — Monday, over the weekend
* @example businessDaysBetween("2024-07-01", "2024-07-31", { weekend: [6, 7], holidays: [], timeZone: "UTC" }) // 22 — July's 23 weekdays less the 1st
* @example businessDaysBetween("2024-07-01", "2024-07-01", { weekend: [6, 7], holidays: [], timeZone: "UTC" }) // 0
* @example businessDaysBetween("2024-07-05", "2024-07-01", { weekend: [6, 7], holidays: ["2024-07-04"], timeZone: "Africa/Kigali" }) // -3 — reversed
* @example businessDaysBetween("2024-07-06", "2024-07-05", { weekend: [6, 7], holidays: [], timeZone: "UTC" }) // 0 — addBusinessDays("2024-07-06", -1) is "2024-07-05", but Saturday is excluded
* @example businessDaysBetween("2024-07-01", "2024-07-08", { weekend: [5, 6], holidays: [], timeZone: "Asia/Riyadh" }) // 5 — Friday–Saturday weekend
* @example businessDaysBetween("invalid", "2024-07-05", { weekend: [6, 7], holidays: [], timeZone: "UTC" }) // null
* @example businessDaysBetween("2024-07-01", "2024-07-05", { weekend: [6, 7], holidays: [], timeZone: "Invalid/Zone" }) // null
*/
function businessDaysBetween(start, end, calendar) {
try {
const resolved = parseBusinessCalendar(calendar);
const startDate = plainDateOf(start);
if (startDate === null) return null;
const endDate = plainDateOf(end);
if (endDate === null || resolved === null) return null;
try {
const startDay = epochDayOfDate(startDate);
const endDay = epochDayOfDate(endDate);
const reversed = startDay > endDay;
const count = reversed ? businessDaysWithin(endDay + 1, startDay, resolved) : businessDaysWithin(startDay + 1, endDay, resolved);
return reversed ? 0 - count : count;
} catch {
return null;
}
} catch {
return null;
}
}
export { businessDaysBetween };