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.

61 lines (59 loc) • 3.77 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"; import { formatRangeRepaired } from "../../internal/rangeFields.js"; // src/plain/format/formatDateRange.ts /** * Format a plain date range using the Temporal Intl.DateTimeFormat formatRange API. * * - Plain counterpart of `formatZonedRange` — same parameter order and option shape. * - Uses Temporal.PlainDate.from for both endpoints; no timezone is involved. * - Formats as Temporal's ECMA-402 PlainDate format does, through the runtime's * `Intl.DateTimeFormat#formatRange`: `timeZoneName` is ignored, and a `timeStyle` without * `dateStyle`, or only time fields, returns `""`. * - **Compatibility:** before 1.16.0 `timeZoneName` alone returned `""`; only that sentinel * became a value. * - Locale elides shared fields between `start` and `end` (e.g. same month/year). * - Returns "" for invalid input on either endpoint. * - 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. 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 start ISO PlainDate string (range start) * @param end ISO PlainDate string (range end) * @param locale optional locale tag (default: runtime default), or a preference list of tags (ECMA-402) * @param options optional Intl.DateTimeFormatOptions * @returns localized range string or "" when invalid * * @example formatDateRange("2024-02-03", "2024-02-05", "en-GB", { dateStyle: "long" }) // "3 - 5 February 2024" * @example formatDateRange("2024-02-03", "2024-06-10", "pt-BR", { dateStyle: "long" }) // "3 de fevereiro - 10 de junho de 2024" * @example formatDateRange("2024-02-03", "2024-02-05", "hi-IN", { timeZoneName: "short" }) // "3/2/2024 - 5/2/2024" * @example formatDateRange("invalid", "2024-02-05", "ja-JP") // "" (invalid input) * @example formatDateRange("2024-02-03", "2024-02-05", ["fr-FR", "en-US"], { dateStyle: "long" }) // "3-5 février 2024" (GMT writes range dashes as ASCII hyphens) */ function formatDateRange(start, end, locale, options) { const startParsed = plainDateOf(start); const endParsed = plainDateOf(end); if (startParsed === null || endParsed === null) return ""; try { const read = readDateTimeFormatOptions(options); checkFormatterArguments("DateTimeFormat", locale, read); const resolved = plainDateFormatOptions(read); if (resolved === null) return ""; const out = formatRangeRepaired(cachedFormatter("DateTimeFormat", locale, resolved), resolved, startParsed.toZonedDateTime("UTC").epochMilliseconds, endParsed.toZonedDateTime("UTC").epochMilliseconds); return out === null ? "" : normalizeDateTime(out); } catch { return ""; } } export { formatDateRange };