UNPKG

@burglekitt/gmt

Version:

Temporal-based date and time utilities with timezone support and polyfill integration

24 lines (23 loc) 903 B
import { normalizeDateTime } from "../../internal/normalizeDateTime.js"; import { normalizeTimeZone } from "../../internal/normalizeTimeZone.js"; import { toInstantFromUtc } from "../../internal/toInstantFromUtc.js"; import { isValidUtc } from "../validate/index.js"; export function formatUtc(value, locale, options) { if (!isValidUtc(value)) return ""; const { timeZone, includeTimeZoneName = false, ...intlOptions } = options ?? {}; const instant = toInstantFromUtc(value); if (instant === null) return ""; try { const tz = normalizeTimeZone(timeZone); const zdt = instant.toZonedDateTimeISO(tz); const out = includeTimeZoneName ? zdt.toLocaleString(locale, intlOptions) : zdt.toPlainDateTime().toLocaleString(locale, intlOptions); return normalizeDateTime(out); } catch { return ""; } }