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.

75 lines (73 loc) • 4.24 kB
import { Temporal } from "../../internal/temporal.js"; import { plainYearMonthOf } from "../../internal/inputChecks.js"; import { cachedFormatter, checkFormatterArguments } from "../../internal/formatterCache.js"; import { normalizeDateTime } from "../../internal/normalizeDateTime.js"; import { plainYearMonthFormatOptions } from "../../internal/plainFormatOptions.js"; import { readDateTimeFormatOptions } from "../../internal/readDateTimeFormatOptions.js"; // src/plain/format/formatYearMonth.ts /** * Return a localized string for a year-month (`YYYY-MM`) using Intl options. * * - Formats as Temporal's ECMA-402 PlainYearMonth format does: only `year`, `month` and `era` are * kept, at noon UTC on the first of the month. * - **Gregorian rule (GMT).** Always formats through the Gregorian calendar, which names the same * year and month as the ISO one. ICU formats `calendar: "iso8601"` without the locale's * year-month pattern (Node prints `"1970 "` with no month name), and * `PlainYearMonth#toLocaleString` throws for a calendar mismatch. A `-u-ca-` subtag in `locale` * is overridden. With a `calendar` option, the calendar Intl resolves must be `gregory` or * `iso8601`, otherwise `""`. Intl ignores an unsupported value and uses the locale's default, so * `{ calendar: "foo" }` gives `"10/2026"` in en-US and `""` in th-TH (Buddhist). * - **`dateStyle`.** The year and month widths are the locale's own `dateStyle` date pattern's, as * ECMA-402 (AdjustDateTimeStyleFormat) and native `PlainYearMonth#toLocaleString` take them: * en-US `short` is `10/26`, de-DE `medium` `10/2026` and zh-CN `long` `2026/10`. Polyfill 0.5.1 * maps each style to fixed widths instead, so its `toLocaleString` prints `Okt. 2026` for de-DE * `medium`. * - A `day`, `weekday`, time field or `timeZoneName` is dropped; only those fields with no `year` * or `month` give `""`, except `timeZoneName`, which falls back to the defaults. An `era` is * kept. * - A year at or before 0 prints the Gregorian era year (`0000` is year 1, `-000001` is year 2) * with no era unless `era` is asked for, as `formatDate` does. * - Returns `""` for invalid input, a `timeStyle`, invalid options, `null` options or an * ill-formed locale. * * @param value ISO year-month 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 year-month string or "" on invalid input * * @example formatYearMonth("2026-10", "en-US") // "10/2026" * @example formatYearMonth("2026-10", "de-DE", { year: "numeric", month: "long" }) // "Oktober 2026" * @example formatYearMonth("2026-10", "fr-FR", { dateStyle: "medium" }) // "oct. 2026" * @example formatYearMonth("2026-10", "en-US", { era: "short", year: "numeric", month: "long" }) // "October 2026 AD" * @example formatYearMonth("2026-10", "en-US", { year: "numeric", month: "long", day: "numeric" }) // "October 2026" * @example formatYearMonth("2026-10", "en-US", { timeStyle: "short" }) // "" * @example formatYearMonth("2026-10", "en-US", { calendar: "hebrew" }) // "" * @example formatYearMonth("2026-13") // "" */ function formatYearMonth(value, locale, options) { const yearMonth = plainYearMonthOf(value); if (yearMonth === null) return ""; try { const read = readDateTimeFormatOptions(options); checkFormatterArguments("DateTimeFormat", locale, read); if (read.timeStyle !== void 0) return ""; if (read.calendar !== void 0) { const calendar = cachedFormatter("DateTimeFormat", locale, { calendar: read.calendar }).resolvedOptions().calendar; if (calendar !== "gregory" && calendar !== "iso8601") return ""; } const resolved = plainYearMonthFormatOptions(read, locale); if (resolved === null) return ""; const epochMilliseconds = Temporal.PlainDate.from({ year: yearMonth.year, month: yearMonth.month, day: yearMonth.year === -271821 && yearMonth.month === 4 ? 20 : 1 }).toZonedDateTime({ timeZone: "UTC", plainTime: "12:00" }).epochMilliseconds; return normalizeDateTime(cachedFormatter("DateTimeFormat", locale, resolved).format(epochMilliseconds)); } catch { return ""; } } export { formatYearMonth };