UNPKG

@northguild/gmt

Version:

Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.

46 lines • 4.42 kB
import { DateTimeFormatOptions } from "../../types/datetime-format-options.js"; import "../../types/index.js"; /** * Format a zoned datetime range using the Temporal Intl.DateTimeFormat formatRange API. * * - Both datetimes must have the same time zone: identifiers of one zone count as the same * (`UTC` and `Etc/UTC` on every host; an IANA link such as `Europe/Kiev` and `Europe/Kyiv` where * the host resolves it, which Safari 27 without `Temporal` does not). * - 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. * - The requested `timeZoneName` style is kept, also on engines whose ICU drops it from ranges * (ICU-20548), and a flexible `dayPeriod` next to a zone name stays "in the morning" on engines * that write AM/PM in ranges. * - Returns "" for invalid input, mismatched timezones, or a `timeZone` option (a ZonedDateTime * keeps its own zone). * - **Compatibility:** before 1.21.0 on V8 and JSC, `long`, `longGeneric` and the offset zone names * came out short or missing in ranges, and on Firefox a `dayPeriod` with a `timeZoneName` came out * as AM or PM. 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). * * @param from zoned ISO 8601 datetime string (range start) * @param to zoned ISO 8601 datetime string (range end) * @param locale optional locale tag, or a preference list of tags (ECMA-402) * @param options optional Intl.DateTimeFormatOptions * @returns localized range string or "" when invalid * * @example formatZonedRange("2024-02-29T12:00:00.000+00:00[UTC]", "2024-02-29T14:00:00.000+00:00[UTC]", "de-DE", { dateStyle: "long", timeStyle: "short" }) // "29. Februar 2024, 12:00-14:00 Uhr" * @example formatZonedRange("2024-02-29T12:00:00.000+00:00[UTC]", "2024-02-29T14:00:00.000+00:00[UTC]", "en-GB", { dateStyle: "short", timeStyle: "short" }) // "29/02/2024, 12:00-14:00" * @example formatZonedRange("2024-02-03T14:30:45+08:00[Asia/Shanghai]", "2024-02-03T15:30:45+08:00[Asia/Shanghai]", "zh-CN", { year: "numeric", month: "long" }) // "2024年2月" * @example formatZonedRange("2024-02-03T14:30:45+00:00[Europe/London]", "2024-02-03T15:30:45+00:00[Europe/London]", "en-GB", { era: "long" }) // "03/02/2024 Anno Domini, 14:30:45 - 15:30:45" * @example formatZonedRange("2024-02-29T10:00:00+09:00[Asia/Tokyo]", "2024-02-29T12:00:00+09:00[Asia/Tokyo]", "ja-JP", { hour: "numeric", timeZone: "UTC" }) // "" — a ZonedDateTime keeps its own zone * @example formatZonedRange("2024-02-29T10:00:00+00:00[Europe/London]", "2024-02-29T12:00:00+00:00[Europe/London]", "en-GB", { hour: "numeric", minute: "numeric", timeZoneName: "short" }) // "10:00-12:00 GMT" — the pre-1.16.0 text * @example formatZonedRange("2024-02-29T10:00:00-05:00[America/New_York]", "2024-02-29T12:00:00-05:00[America/New_York]", "en-US", { hour: "numeric", minute: "numeric", timeZoneName: "long" }) // "10:00 AM - 12:00 PM Eastern Standard Time" * @example formatZonedRange("invalid", "2024-02-29T14:00:00.000+00:00[UTC]", "fr-FR") // "" (invalid input) * @example formatZonedRange("2024-02-03T14:30:45+01:00[Europe/Paris]", "2024-02-03T16:46:15+01:00[Europe/Paris]", ["fr-FR", "en-US"], { dateStyle: "short", timeStyle: "short" }) // "03/02/2024, 14:30 - 16:46" * @example formatZonedRange("2024-02-03T14:30:00-03:00[America/Sao_Paulo]", "2024-02-05T17:00:00-03:00[America/Sao_Paulo]", "pt-BR", null as never) // "" (null options, as ECMA-402 rejects them) */ export declare function formatZonedRange(from: string, to: string, locale?: string | string[], options?: DateTimeFormatOptions): string;