@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.
65 lines (63 loc) • 3.92 kB
JavaScript
import { optionOrDefault } from "../../internal/optionOrDefault.js";
import { normalizeDateTime } from "../../internal/normalizeDateTime.js";
import { readDateTimeFormatOptions } from "../../internal/readDateTimeFormatOptions.js";
import { normalizeTimeZone } from "../../internal/normalizeTimeZone.js";
import { isValidUtc } from "../validate/isValidUtc.js";
import { formatWallClockOrZoned } from "../../internal/instantFormatOptions.js";
import { toInstantFromUtc } from "../../internal/toInstantFromUtc.js";
// src/utc/format/formatUtc.ts
/**
* Format a UTC ISO string as a localized date/time string.
*
* - Returns `""` if the input is not a valid UTC string.
* - The requested fields and style widths are kept, and `era` alone still gets the date and time
* defaults.
* - **Compatibility:** before 1.16.0 some locales and calendars lost a requested width (ja-JP with
* the japanese calendar and `month: "long"` gave `"R6/2"`), a `long`/`full` `timeStyle` replaced
* the `dateStyle` width, `era` alone dropped the time, and `timeZoneName` alone without
* `includeTimeZoneName` returned `""`. Pass the fields the old text showed to keep it.
* - `options` null returns `""`, as ECMA-402's CoerceOptionsToObject rejects it.
* - An offset with seconds (`-00:44:30`) returns `""`. `Intl.DateTimeFormat` takes a time zone
* identifier only: constructing one with an offset that has seconds throws (ECMA-402, the
* `DateTimeFormat` construction step "If parseResult contains more than one MinuteSecond Parse
* Node, throw a RangeError exception"). Pass the IANA name.
*
* @param value UTC ISO string to format
* @param locale optional: BCP 47 locale tag, or a preference list of tags (ECMA-402)
* @param options The time zone, the zone-name switch and any `Intl.DateTimeFormatOptions`
* @returns the formatted date/time string, or "" on invalid input
*
* @example formatUtc("2026-03-16T18:30:00Z") // "3/16/2026, 6:30:00 PM" (varies by the runtime's default locale; shown for en-US)
* @example formatUtc("2026-03-16T18:30:00Z", "ja-JP", { timeZone: "Asia/Tokyo" }) // "2026/3/17 3:30:00"
* @example formatUtc("2026-03-16T18:30:00Z", "ja-JP", { timeZone: "Asia/Tokyo", includeTimeZoneName: true }) // "2026/3/17 3:30:00 JST"
* @example formatUtc("2024-02-03T14:30:45Z", "ja-JP-u-ca-japanese", { year: "numeric", month: "long" }) // "令和6年2月"
* @example formatUtc("2024-02-03T14:30:45Z", "ja-JP-u-ca-japanese", { year: "numeric", month: "numeric" }) // "R6/2" — the pre-1.16.0 text
* @example formatUtc("2024-02-03T14:30:45Z", "en-GB", { era: "long" }) // "03/02/2024 Anno Domini, 14:30:45"
* @example formatUtc("2024-02-03T14:30:45Z", "en-GB", { era: "long", year: "numeric", month: "numeric", day: "numeric" }) // "03/02/2024 Anno Domini" — the pre-1.16.0 text
* @example formatUtc("2024-02-03T14:30:45Z", "de-DE", { timeZoneName: "short" }) // "3.2.2024, 14:30:45"
* @example formatUtc("2024-02-03T14:30:45Z", "zh-CN", { timeZone: "Europe/Londn" }) // "" (unknown zone)
* @example formatUtc("2024-02-03T14:30:45Z", "pt-BR", null as never) // "" (null options, as ECMA-402 rejects them)
* @example formatUtc("not-a-date") // ""
* @example formatUtc("2024-02-03T14:30:45Z", ["fr-FR", "en-US"], { dateStyle: "long" }) // "3 février 2024"
*/
function formatUtc(value, locale, options) {
try {
if (options === null) return "";
if (!isValidUtc(value)) return "";
const includeTimeZoneName = optionOrDefault(options?.includeTimeZoneName, false);
const { timeZone, ...intlOptions } = readDateTimeFormatOptions(options);
const instant = toInstantFromUtc(value);
if (instant === null) return "";
const tz = normalizeTimeZone(timeZone);
if (!tz) return "";
try {
const zdt = instant.toZonedDateTimeISO(tz);
return normalizeDateTime(formatWallClockOrZoned(zdt, locale, intlOptions, includeTimeZoneName));
} catch {
return "";
}
} catch {
return "";
}
}
export { formatUtc };