@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.
63 lines (61 loc) • 4.46 kB
JavaScript
import { plainDateTimeOf } from "../../internal/inputChecks.js";
import { cachedFormatter, checkFormatterArguments } from "../../internal/formatterCache.js";
import { normalizeDateTime } from "../../internal/normalizeDateTime.js";
import { plainDateTimeFormatOptions } from "../../internal/plainFormatOptions.js";
import { readDateTimeFormatOptions } from "../../internal/readDateTimeFormatOptions.js";
import { formatRangeRepaired } from "../../internal/rangeFields.js";
// src/plain/format/formatDateTimeRange.ts
/**
* Format a plain datetime range using the Temporal Intl.DateTimeFormat formatRange API.
*
* - Plain counterpart of `formatZonedRange` — same parameter order and option shape.
* - Uses Temporal.PlainDateTime.from for both endpoints; no timezone is involved.
* - Formats as Temporal's ECMA-402 PlainDateTime format does, through the runtime's
* `Intl.DateTimeFormat#formatRange`: the requested fields and style widths are kept, `era`
* alone still gets the date and time defaults, and `timeZoneName` is ignored.
* - **Compatibility:** before 1.16.0 some locales lost a requested width (zh-CN with
* `{ year: "numeric", month: "long" }` gave `"2024/2"`), `era` alone dropped the time, and
* `timeZoneName` alone returned `""`. Pass the fields the old text showed to keep it.
* - Locale elides shared fields between `start` and `end` (e.g. same day/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 time or weekday
* field always returns its text.
*
* @param start ISO PlainDateTime string (range start)
* @param end ISO PlainDateTime 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 formatDateTimeRange("2024-02-03T09:00:00", "2024-02-03T17:00:00", "en-GB", { dateStyle: "long", timeStyle: "short" }) // "3 February 2024, 09:00-17:00"
* @example formatDateTimeRange("2024-02-03T09:00:00", "2024-02-10T17:00:00", "pt-BR", { dateStyle: "long", timeStyle: "short" }) // "3 de fevereiro de 2024 às 09:00 - 10 de fevereiro de 2024 às 17:00"
* @example formatDateTimeRange("2024-02-03T09:00:00", "2024-02-03T17:00:00", "zh-CN", { year: "numeric", month: "long" }) // "2024年2月"
* @example formatDateTimeRange("2024-02-03T09:00:00", "2024-02-03T17:00:00", "zh-CN", { year: "numeric", month: "numeric" }) // "2024/2" — the pre-1.16.0 text
* @example formatDateTimeRange("2024-02-03T09:00:00", "2024-02-03T17:00:00", "en-GB", { era: "long" }) // "03/02/2024 Anno Domini, 09:00:00 - 17:00:00"
* @example formatDateTimeRange("invalid", "2024-02-03T17:00:00", "de-DE") // "" (invalid input)
* @example formatDateTimeRange("2024-02-03T14:30:45", "2024-02-03T16:46:15", ["fr-FR", "en-US"], { hour: "numeric", minute: "numeric" }) // "14:30 - 16:46"
*/
function formatDateTimeRange(start, end, locale, options) {
const startParsed = plainDateTimeOf(start);
const endParsed = plainDateTimeOf(end);
if (startParsed === null || endParsed === null) return "";
try {
const read = readDateTimeFormatOptions(options);
checkFormatterArguments("DateTimeFormat", locale, read);
const formatOptions = plainDateTimeFormatOptions(read);
const out = formatRangeRepaired(cachedFormatter("DateTimeFormat", locale, formatOptions), formatOptions, startParsed.toZonedDateTime("UTC").epochMilliseconds, endParsed.toZonedDateTime("UTC").epochMilliseconds);
return out === null ? "" : normalizeDateTime(out);
} catch {
return "";
}
}
export { formatDateTimeRange };