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.

62 lines (60 loc) • 3.97 kB
import { plainDateOf } from "../../internal/inputChecks.js"; import { cachedFormatter, checkFormatterArguments } from "../../internal/formatterCache.js"; import { normalizeDateTime } from "../../internal/normalizeDateTime.js"; import { plainDateFormatOptions } from "../../internal/plainFormatOptions.js"; import { readDateTimeFormatOptions } from "../../internal/readDateTimeFormatOptions.js"; // src/plain/format/formatDate.ts /** * Return a localized string for a PlainDate ISO input using Intl options. * * - Formats as Temporal's ECMA-402 PlainDate format (`PlainDate#toLocaleString`) does, through * the runtime's `Intl.DateTimeFormat`: the requested fields and widths are kept, and * `timeZoneName` is ignored (a PlainDate has no zone). * - Accepts optional BCP 47 locale and Intl.DateTimeFormatOptions. * - Returns "" for invalid input, for a `timeStyle` (with or without `dateStyle`) and for only * time fields, where `PlainDate#toLocaleString` throws a TypeError. * - **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"`), `timeZoneName` alone returned `""`, * and a `timeStyle` beside `dateStyle` was ignored. Pass the numeric fields to keep the numeric * text, and pass only `dateStyle` to keep the styled text. * - `options` null returns `""`, as ECMA-402's CoerceOptionsToObject rejects it. Any * other non-object — a string, a number, a boolean — formats with the defaults, as * `Intl.DateTimeFormat` does: CoerceOptionsToObject calls ToObject on it and the wrapper * carries no recognised option. Only `null` and `undefined` are special-cased. * - A `chinese` or `dangi` date follows the observatory the standard names: Purple Mountain * Observatory for `chinese`, KASI for `dangi`. A runtime that prints a different date for one of * them returns `""` for any format with a year, month, day or era in that calendar. A runtime * that prints the observatory's date keeps its own text. A format with only a weekday * field always returns its text. * * @param value ISO PlainDate string * @param locale optional BCP 47 locale identifier (default: runtime default), or a preference list of tags (ECMA-402) * @param options optional Intl.DateTimeFormatOptions * @returns localized date string or "" on invalid input * * @example formatDate("2024-03-15", "pt-BR", { year: "numeric", month: "long", day: "numeric" }) // "15 de março de 2024" * @example formatDate("2024-03-15", "de-DE", { year: "numeric", month: "long", day: "numeric" }) // "15. März 2024" * @example formatDate("2024-02-03", "ja-JP-u-ca-japanese", { year: "numeric", month: "long" }) // "令和6年2月" * @example formatDate("2024-02-03", "ja-JP-u-ca-japanese", { year: "numeric", month: "numeric" }) // "R6/2" — the pre-1.16.0 text * @example formatDate("2024-02-03", "hi-IN", { timeZoneName: "long" }) // "3/2/2024" * @example formatDate("2024-02-03", "en-GB", { dateStyle: "short", timeStyle: "short" }) // "" — a PlainDate has no time * @example formatDate("2024-02-03", "en-GB", { dateStyle: "short" }) // "03/02/2024" — the pre-1.16.0 text * @example formatDate("invalid") // "" * @example formatDate("2024-02-03", ["fr-FR", "en-US"], { year: "numeric", month: "2-digit", day: "2-digit" }) // "03/02/2024" (the first listed locale) */ function formatDate(value, locale, options) { const valueParsed = plainDateOf(value); if (valueParsed === null) return ""; try { const read = readDateTimeFormatOptions(options); checkFormatterArguments("DateTimeFormat", locale, read); if (read.timeStyle !== void 0) return ""; const resolved = plainDateFormatOptions(read); if (resolved === null) return ""; const epochMilliseconds = valueParsed.toZonedDateTime("UTC").epochMilliseconds; return normalizeDateTime(cachedFormatter("DateTimeFormat", locale, resolved).format(epochMilliseconds)); } catch { return ""; } } export { formatDate };