Skip to content

intervalCountDate

intervalCountDate(start: string, end: string, unit: string): number | null

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.
ParameterTypeDescription
startstringISO PlainDate string for the interval start, optionally calendar-annotated
endstringISO PlainDate string for the interval end, optionally calendar-annotated
unitstringunit string — "year" | "month" | "week" | "day" (time units return null)

number of unit boundaries touched, or null on invalid input

import { intervalCountDate } from "@northguild/gmt/plain/interval";
intervalCountDate("2024-01-01", "2024-01-03", "day") // 2
output 

plain/interval/intervalCountDate.ts