Skip to content

splitIntervalByUnitDateTime

splitIntervalByUnitDateTime(start: string, end: string, unit: string, amount: number): { start: string; end: string; }[]
import { splitIntervalByUnitDateTime } from "@northguild/gmt/plain/interval";

Split a date-time interval into sub-intervals of amount × unit.

  • Returns an array of { start, end } records that tile the interval.
  • The final sub-interval is trimmed so its end never exceeds the original end.
  • Returns [{ start, end }] when start === end (zero-length interval).
  • Returns [] on invalid input (unparseable start/end, unsupported unit, non-positive amount).
Parameter Type Description
start string ISO PlainDateTime string for the interval start
end string ISO PlainDateTime string for the interval end
unit string duration unit string — any DateTimeDurationUnit
amount number positive number of units per step

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

splitIntervalByUnitDateTime("2024-01-01T12:00:00", "2024-01-01T14:00:00", "hour", 1) // [{ start: "2024-01-01T12:00:00", end: "2024-01-01T13:00:00" }, { start: "2024-01-01T13:00:00", end: "2024-01-01T14:00:00" }]
splitIntervalByUnitDateTime("2024-01-01T12:00:00", "2024-01-01T14:30:00", "hour", 1) // [{ start: "2024-01-01T12:00:00", end: "2024-01-01T13:00:00" }, { start: "2024-01-01T13:00:00", end: "2024-01-01T14:00:00" }, { start: "2024-01-01T14:00:00", end: "2024-01-01T14:30:00" }]
splitIntervalByUnitDateTime("2024-01-01T12:00:00", "2024-01-01T12:00:00", "hour", 1) // [{ start: "2024-01-01T12:00:00", end: "2024-01-01T12:00:00" }]
splitIntervalByUnitDateTime("2024-01-01T12:00:00", "2024-01-01T14:00:00", "hour", 0) // []
splitIntervalByUnitDateTime("invalid", "2024-01-01T14:00:00", "hour", 1) // []

plain/interval/splitIntervalByUnitDateTime.ts