@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.
36 lines • 2.63 kB
TypeScript
/**
* Count how many `unit` boundaries a date interval crosses.
*
* - Counts calendar boundaries touched by the half-open interval `[start, end)` — distinct
* from `diffDate`, which measures exact elapsed duration.
* - `"2024-01-01"` to `"2024-01-03"` counted in days is 2: the end boundary is excluded.
* - A zero-length interval (`start === end`) returns `0`: the empty `[start, start)` holds no instant,
* so it touches no unit (before 1.16.0 it counted 1 when mid-unit).
* - Weeks start on Monday (ISO 8601).
* - A unit that began before the first representable date (`-271821-04-19`) is still counted:
* whole units are measured from the unit after `start`'s, never from `start`'s own start.
* - Accepts singular or plural units (`"day"` and `"days"` behave identically).
* - Returns `null` on invalid input (unparseable start/end, `start > end`, unsupported unit,
* or a unit that has no effect on `PlainDate`, e.g. `"hours"`).
* - Accepts RFC 9557 calendar-annotated PlainDate strings — E5 (issue #78). When `start` and `end`
* carry the *same* calendar tag, boundaries are counted in that calendar (a Hebrew leap year
* crosses 13 month boundaries, not 12). When they name different calendars (a bare ISO string
* names `iso8601`) the result is `null`, as Temporal's `until` throws when `CalendarEquals` is
* false (before 1.16.0 it counted in ISO).
* - Compatibility: since 1.16.0 calendar strings are RFC 9557 (ISO digits, `[u-ca=<id>]`, canonical
* calendar ids); see `isValidCalendarDate`.
*
* @param start ISO PlainDate string for the interval start, optionally calendar-annotated
* @param end ISO PlainDate string for the interval end, optionally calendar-annotated
* @param unit unit string — `"year" | "month" | "week" | "day"` (time units return null)
* @returns number of unit boundaries touched, or null on invalid input
*
* @example intervalCountDate("2024-01-01", "2024-01-03", "day") // 2
* @example intervalCountDate("2024-01-15", "2024-03-10", "month") // 3
* @example intervalCountDate("2024-01-15", "2024-01-15", "month") // 0 (zero-length: holds no instant)
* @example intervalCountDate("2024-01-01", "2024-01-10", "hour") // null
* @example intervalCountDate("invalid", "2024-01-10", "day") // null
* @example intervalCountDate("2023-09-16[u-ca=hebrew]", "2024-10-03[u-ca=hebrew]", "month") // 13 (Hebrew leap year, measured in Hebrew)
* @example intervalCountDate("2024-10-03[u-ca=hebrew]", "2024-11-03", "month") // null (different calendars)
*/
export declare function intervalCountDate(start: string, end: string, unit: string): number | null;