Skip to content

intervalSplitAtDateTime

intervalSplitAtDateTime(start: string, end: string, points: string[]): { start: string; end: string; }[]

Split a datetime interval at arbitrary points, producing consecutive sub-intervals.

  • points need not be sorted — they are sorted internally before splitting.
  • Points before start or after end are dropped; they cannot introduce a boundary that isn’t inside the interval.
  • Points exactly on start or end are dropped too — they would only produce a zero-length sub-interval at the edge; intervalDivideEquallyDateTime’s zero-length case is the deliberate way to express one, not intervalSplitAtDateTime.
  • Duplicate points collapse to a single boundary.
  • Returns consecutive { start, end } records, each record’s end equal to the next record’s start.
  • Every piece is half-open [start, end): a boundary belongs only to the piece that starts there, so the pieces share no value and together cover the interval exactly once (the rule CORE-6’s splitIntervalAt uses).
  • Returns [{ start, end }] (the whole interval, unsplit) when no valid in-range point remains.
  • A piece’s end equals its start only when the interval itself is zero-length (start equal to end): the result is then that one empty piece, whatever the points.
  • Returns [] when points is not an array, when any element is not a valid ISO PlainDateTime string, or on invalid input (unparseable start/end, start > end).
ParameterTypeDescription
startstringISO PlainDateTime string for the interval start
endstringISO PlainDateTime string for the interval end
pointsstring[]array of ISO PlainDateTime strings to split at

array of { start, end } records, or [] on invalid input

Members of each item

MemberTypeDescription
startstringThe date-time the interval begins at, as an ISO 8601 date-time string with no offset or time zone.
endstringThe first date-time after the interval, in the same format as start. It is exclusive: the interval holds everything from start up to but not including this value. It can equal start, which makes the interval empty.
import { intervalSplitAtDateTime } from "@northguild/gmt/plain/interval";
intervalSplitAtDateTime("2024-01-01T00:00:00", "2024-01-10T00:00:00", ["2024-01-05T00:00:00"]) // [{ start: "2024-01-01T00:00:00", end: "2024-01-05T00:00:00" }, { start: "2024-01-05T00:00:00", end: "2024-01-10T00:00:00" }]
points
output 

plain/interval/intervalSplitAtDateTime.ts