intervalOverlappingDaysDateTime
Signature
Section titled “Signature”intervalOverlappingDaysDateTime(aStart: string, aEnd: string, bStart: string, bEnd: string): numberimport { intervalOverlappingDaysDateTime } from "@northguild/gmt/plain/interval";Return how many distinct calendar dates two datetime intervals share.
- Counts the number of local dates touched by the closed intersection
[max(aStart, bStart), min(aEnd, bEnd)]— inclusive of both endpoints. - Adjacent intervals (e.g.
aEnd === bStart) share one date and count as1. - Returns
0when the intervals do not overlap at all (a well-defined answer, not invalid input). - Returns
nullif either interval is invalid (start > end). - Returns
nullon invalid input (wrong type, malformed strings). - Diverges from date-fns’s
getOverlappingDaysInIntervals, which rounds up elapsed 24-hour periods instead of counting calendar dates (its own doc example — Jan 10-20 vs Jan 17-21 — returns 3 there, 4 here). To reproduce date-fns’s number, composeintervalIntersectionDateTimewithintervalCountDateTime:const span = intervalIntersectionDateTime(aStart, aEnd, bStart, bEnd); span ? intervalCountDateTime(span.start, span.end, "day") : 0;
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
aStart |
string |
ISO 8601 datetime string for the first interval start |
aEnd |
string |
ISO 8601 datetime string for the first interval end |
bStart |
string |
ISO 8601 datetime string for the second interval start |
bEnd |
string |
ISO 8601 datetime string for the second interval end |
Returns
Section titled “Returns”number of shared calendar dates, 0 when disjoint, or null on invalid input
Examples
Section titled “Examples”intervalOverlappingDaysDateTime("2024-01-01T23:59:00", "2024-01-02T00:01:00", "2024-01-01T23:59:00", "2024-01-02T00:01:00") // 2intervalOverlappingDaysDateTime("2024-01-01T00:00:00", "2024-01-02T00:00:00", "2024-01-02T00:00:00", "2024-01-03T00:00:00") // 1 (adjacent)intervalOverlappingDaysDateTime("2024-01-01T00:00:00", "2024-01-02T00:00:00", "2024-01-02T00:00:00.001", "2024-01-03T00:00:00") // 0 (disjoint)intervalOverlappingDaysDateTime("invalid", "2024-06-30T23:59:59", "2024-04-01T00:00:00", "2024-12-31T23:59:59") // null