intervalCountDate
Signature
Section titled “Signature”intervalCountDate(start: string, end: string, unit: string): number | nullCount how many unit boundaries a date interval crosses.
- Counts calendar boundaries touched by the half-open interval
[start, end)— distinct fromdiffDate, 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) returns0: 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 afterstart’s, never fromstart’s own start. - Accepts singular or plural units (
"day"and"days"behave identically). - Returns
nullon invalid input (unparseable start/end,start > end, unsupported unit, or a unit that has no effect onPlainDate, e.g."hours"). - Accepts RFC 9557 calendar-annotated PlainDate strings — E5 (issue #78). When
startandendcarry 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 namesiso8601) the result isnull, as Temporal’suntilthrows whenCalendarEqualsis 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); seeisValidCalendarDate.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
start | string | ISO PlainDate string for the interval start, optionally calendar-annotated |
end | string | ISO PlainDate string for the interval end, optionally calendar-annotated |
unit | string | unit string — "year" | "month" | "week" | "day" (time units return null) |
Returns
Section titled “Returns”number of unit boundaries touched, or null on invalid input
Examples
Section titled “Examples”import { intervalCountDate } from "@northguild/gmt/plain/interval";
intervalCountDate("2024-01-01", "2024-01-03", "day") // 2