Skip to content

formatZonedRange

formatZonedRange(from: string, to: string, locale?: string | string[], options?: DateTimeFormatOptions): string

Format a zoned datetime range using the Temporal Intl.DateTimeFormat formatRange API.

  • Both datetimes must have the same timezone.
  • Formats both instants as Temporal’s ECMA-402 Instant format does, through the runtime’s Intl.DateTimeFormat#formatRange in the endpoints’ zone: the requested fields and widths are kept, and with no date or time field (era alone included) the date and time are the defaults.
  • Returns "" for invalid input, mismatched timezones, or a timeZone option (a ZonedDateTime keeps its own zone).
  • Compatibility: before 1.16.0 some locales lost a requested width (zh-CN with { year: "numeric", month: "long" } gave "2024/2"), era alone dropped the time, and a timeZone option was silently overridden by the endpoints’ zone. Pass the fields the old text showed to keep it, and drop the timeZone option to keep the endpoints’ zone.
  • Output is normalized: dash separators become ASCII ”-” (unspaced between digits, spaced otherwise), and no-break, narrow and thin spaces become U+0020.
  • options null returns "", as ECMA-402’s CoerceOptionsToObject rejects it (a string or number options value formats with the defaults, as Intl.DateTimeFormat does).
ParameterTypeDescription
fromstringzoned ISO 8601 datetime string (range start)
tostringzoned ISO 8601 datetime string (range end)
localestring | string[]optional locale tag, or a preference list of tags (ECMA-402)

options

optional Intl.DateTimeFormatOptions

Type: DateTimeFormatOptions.

OptionTypeDefaultDescription
…Intl.DateTimeFormatOptions——Every field of DateTimeFormatOptions, as ECMA-402 defines it.

localized range string or "" when invalid

import { formatZonedRange } from "@northguild/gmt/zoned/format";
formatZonedRange("2024-02-29T12:00:00.000+00:00[UTC]", "2024-02-29T14:00:00.000+00:00[UTC]", "en-US", { dateStyle: "long", timeStyle: "short" }) // "February 29, 2024, 12:00 - 2:00 PM"
output 

zoned/format/formatZonedRange.ts