@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.
54 lines (52 loc) • 3.27 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { isValidDate } from "../validate/isValidDate.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/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.
*
* @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) {
if (!isValidDate(start) || !isValidDate(end)) return "";
try {
const read = readDateTimeFormatOptions(options);
checkFormatterArguments("DateTimeFormat", locale, read);
const resolved = plainDateFormatOptions(read);
if (resolved === null) return "";
const out = cachedFormatter("DateTimeFormat", locale, resolved).formatRange(Temporal.PlainDate.from(start).toZonedDateTime("UTC").epochMilliseconds, Temporal.PlainDate.from(end).toZonedDateTime("UTC").epochMilliseconds);
return normalizeDateTime(out);
} catch {
return "";
}
}
export { formatDateRange };