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.

34 lines • 2.43 kB
import { DateTimeFormatOptions } from "../../types/datetime-format-options.js"; import "../../types/index.js"; /** * 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) */ export declare function formatDateRange(start: string, end: string, locale?: string | string[], options?: DateTimeFormatOptions): string;