intervalLengthDate
Signature
Section titled “Signature”intervalLengthDate(start: string, end: string, unit: string): number | nullReturn the exact length of a date interval in unit, as a real (possibly fractional) number.
- Distinct from
intervalCountDate, which counts calendarunitboundaries crossed rather than measuring exact duration — seeintervalCountDate’s JSDoc for the canonical 11:59pm→12:01am example of the two diverging.intervalLengthDateanswers “how long is this interval”,intervalCountDateanswers “how many boundaries does it touch”. - Uses
Temporal.Duration.prototype.totalsemantics, which resolve calendar units (month, year) against the interval’s own start so a partial month is expressed as a true fraction rather than truncated (non-ISO calendars follow the same TC39 NudgeToCalendarUnit steps over calendar-correct arithmetic). - Returns
0for a zero-length interval (start === end). - 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, the length is measured in that calendar; when they name different calendars the result isnull— same calendar rule asintervalCountDate(TC39CalendarEquals; before 1.16.0 measured 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”exact length of the interval expressed in unit, or null on invalid input
Examples
Section titled “Examples”import { intervalLengthDate } from "@northguild/gmt/plain/interval";
intervalLengthDate("2024-01-01", "2024-01-03", "day") // 2