Skip to content

intervalOverlappingDaysUnix

intervalOverlappingDaysUnix(aStart: string | number, aEnd: string | number, bStart: string | number, bEnd: string | number, options?: { epochUnit?: UnixUnit; timeZone?: string; }): number | null

Return how many distinct calendar dates two Unix epoch intervals share, in a given time zone.

  • Counts the local dates that hold at least one instant of the half-open intersection [max(aStart, bStart), min(aEnd, bEnd)). The end is excluded, so an intersection ending at local midnight does not touch the date that midnight starts.
  • Each instant in the intersection contributes its own local date in timeZone, and each date counts once. A date the zone deleted is not counted (Pacific/Apia skipped 2011-12-30), and a fall-back into the previous date does not lose that date (America/Goose_Bay, 2010-11-07).
  • Returns null when the intersection crosses more than 10,000 zone transitions (the same cap as intervalCountZoned).
  • Compatibility: before 1.16.0 the count was the calendar difference of the two endpoint dates plus one. To get that number for overlapping intervals, with start/end the intersection: diffDate(convertUnixToPlainDate(start, { timeZone }), convertUnixToPlainDate(end, { timeZone }), "days") + 1.
  • Returns 0 when the intersection is empty (a well-defined answer, not invalid input): when the intervals are disjoint, when they touch (e.g. aEnd === bStart), or when either is empty.
  • Returns null if either interval is invalid (start > end).
  • Returns null on invalid input (epoch values that are not safe integers or numeric strings of one — fractions, empty strings and values beyond ±(2^53 − 1) are invalid — or an invalid timeZone).
  • Diverges from date-fns’s getOverlappingDaysInIntervals, which rounds up elapsed 24-hour periods instead of counting calendar dates. To reproduce date-fns’s number, compose intervalIntersectionUnix with intervalCountUnix: const span = intervalIntersectionUnix(aStart, aEnd, bStart, bEnd); span ? intervalCountUnix(span.start, span.end, "day") : 0;
  • Limit at an offset with seconds. The instant is placed in the offset’s whole-minute zone, moved by its seconds, and the moved instant must be inside Temporal’s range. So within the offset’s seconds (under a minute) of the last instant Temporal supports (+275760-09-13T00:00:00Z) for an offset east of UTC, or of the first (-271821-04-20T00:00:00Z) for one west, this returns null. An offset to the minute has no such limit.
ParameterTypeDescription
aStartstring | numberUnix epoch value (seconds or milliseconds) — first interval start
aEndstring | numberUnix epoch value (seconds or milliseconds) — first interval end
bStartstring | numberUnix epoch value (seconds or milliseconds) — second interval start
bEndstring | numberUnix epoch value (seconds or milliseconds) — second interval end

options

optional: how the epochs are read and the zone the dates are counted in

OptionTypeDefaultDescription
epochUnit?"seconds" | "milliseconds" | "second" | "millisecond" (UnixUnit)"milliseconds"The unit the epoch values are counted in: "seconds" or "milliseconds", singular or plural. Any other value returns null.
timeZone?string"UTC"The time zone the calendar dates are counted in: an IANA name, a UTC offset (a time zone identifier such as +05:30, +0530 or -08, or a stored offset ±HH:MM[:SS], what getTimeZoneOffset returns), or "local" for the system time zone. An unknown zone returns null.

number of shared calendar dates, 0 when disjoint, or null on invalid input

import { intervalOverlappingDaysUnix } from "@northguild/gmt/unix/interval";
intervalOverlappingDaysUnix(0, 172800000, 86400000, 259200000, { timeZone: "UTC" }) // 1 (the intersection [86400000, 172800000) is 1970-01-02)
output 

unix/interval/intervalOverlappingDaysUnix.ts