formatZonedRange
Signature
Section titled “Signature”formatZonedRange(from: string, to: string, locale?: string | string[], options?: DateTimeFormatOptions): stringFormat 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#formatRangein the endpoints’ zone: the requested fields and widths are kept, and with no date or time field (eraalone included) the date and time are the defaults. - Returns "" for invalid input, mismatched timezones, or a
timeZoneoption (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"),eraalone dropped the time, and atimeZoneoption was silently overridden by the endpoints’ zone. Pass the fields the old text showed to keep it, and drop thetimeZoneoption 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.
optionsnull returns"", as ECMA-402’s CoerceOptionsToObject rejects it (a string or number options value formats with the defaults, asIntl.DateTimeFormatdoes).
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
from | string | zoned ISO 8601 datetime string (range start) |
to | string | zoned ISO 8601 datetime string (range end) |
locale | string | string[] | optional locale tag, or a preference list of tags (ECMA-402) |
Options
Section titled “Options”options
optional Intl.DateTimeFormatOptions
Type: DateTimeFormatOptions.
| Option | Type | Default | Description |
|---|---|---|---|
…Intl.DateTimeFormatOptions | — | — | Every field of DateTimeFormatOptions, as ECMA-402 defines it. |
Returns
Section titled “Returns”localized range string or "" when invalid
Related types
Section titled “Related types”Examples
Section titled “Examples”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"