Skip to content

isValidZonedDateTime

isValidZonedDateTime(value: string): boolean

Validate whether a string is a valid ISO 8601 zoned datetime string.

  • The part before the first annotation must be ISO 8601 extended format: <date>T<time>, then nothing, Z, or a ±HH:MM[:SS[.fraction]] offset. Basic format (20241003T143000-0400), a space or lower-case t separator, a lower-case z, an hour-only time or offset, and a date without a time are rejected, although Temporal.ZonedDateTime.from reads them.
  • Uses Temporal.ZonedDateTime.from for validation.
  • A written offset must agree with the bracketed zone, as TC39 ToTemporalZonedDateTime matches it: an offset written to the minute may be the zone’s sub-minute offset rounded (-00:45[Africa/Monrovia] for −00:44:30, which is how Temporal writes it), and an offset written with seconds must be the zone’s offset exactly.
  • Rejects leap seconds.
  • With an offset, the local date must be inside Temporal’s date range, as Temporal.ZonedDateTime.from requires (CheckISODaysRange): a zoned string on −271821-04-19, which a zone west of Greenwich shows for the first hours of the instant range, is rejected although isValidInstant accepts it.
  • Reads RFC 9557 annotations as Temporal.ZonedDateTime.from does: elective ones ([foo=bar]) are ignored and an unknown critical one ([!foo=bar]) is rejected.
  • The calendar must be ISO: [u-ca=iso8601] is accepted, and a non-ISO calendar annotation is isValidCalendarZonedDateTime’s input.
  • Returns false for non-strings or empty strings.
ParameterTypeDescription
valuestringcandidate zoned datetime string

boolean indicating validity

import { isValidZonedDateTime } from "@northguild/gmt/zoned/validate";
isValidZonedDateTime("2024-02-29T12:34:56.789+00:00[UTC]") // true
output 

zoned/validate/isValidZonedDateTime.ts