Skip to content

intervalXorDate

intervalXorDate(aStart: string, aEnd: string, bStart: string, bEnd: string): { start: string; end: string; }[]
import { intervalXorDate } from "@northguild/gmt/plain/interval";

Return the symmetric difference of two date intervals — time covered by exactly one interval.

  • Uses Temporal.PlainDate.compare for comparison.
  • Returns [] when intervals are identical or both invalid.
  • Returns [{ start, end }] when one interval fully contains the other.
  • Returns [{ start, end }, { start, end }] when intervals partially overlap (two non-overlapping pieces).
  • Returns [] if either interval is invalid (start > end).
  • Returns [] on invalid input (wrong type, malformed strings).
  • Accepts GMT calendar-annotated PlainDate strings — E5 (issue #78). Since the result is date values, all four arguments must carry the same calendar tag (or all be bare ISO); a mismatch returns [] (E5 decision of record D4). Each output piece’s tag is re-derived, never copied from an input.
Parameter Type Description
aStart string ISO 8601 date string for the first interval start, optionally calendar-annotated
aEnd string ISO 8601 date string for the first interval end, optionally calendar-annotated
bStart string ISO 8601 date string for the second interval start, optionally calendar-annotated
bEnd string ISO 8601 date string for the second interval end, optionally calendar-annotated

array of { start, end } records representing the symmetric difference, or [] on invalid input / mismatched calendars

intervalXorDate("2024-01-01", "2024-06-30", "2024-04-01", "2024-12-31") // [{ start: "2024-01-01", end: "2024-03-31" }, { start: "2024-07-01", end: "2024-12-31" }]
intervalXorDate("2024-01-01", "2024-12-31", "2024-04-01", "2024-06-30") // [{ start: "2024-01-01", end: "2024-03-31" }, { start: "2024-07-01", end: "2024-12-31" }]
intervalXorDate("2024-01-01", "2024-12-31", "2024-01-01", "2024-12-31") // []
intervalXorDate("2024-01-01", "2024-06-30", "2024-07-01", "2024-12-31") // [{ start: "2024-01-01", end: "2024-06-30" }, { start: "2024-07-01", end: "2024-12-31" }]
intervalXorDate("invalid", "2024-06-30", "2024-07-01", "2024-12-31") // []

plain/interval/intervalXorDate.ts