Skip to content

formatDateTimeRange

formatDateTimeRange(start: string, end: string, locale?: string | string[], options?: DateTimeFormatOptions): string

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.
ParameterTypeDescription
startstringISO PlainDateTime string (range start)
endstringISO PlainDateTime string (range end)
localestring | string[]optional locale tag (default: runtime default), or a preference list of tags (ECMA-402)

options

optional Intl.DateTimeFormatOptions

Type: DateTimeFormatOptions.

OptionTypeDefaultDescription
…Intl.DateTimeFormatOptions——Every field of DateTimeFormatOptions, as ECMA-402 defines it.

localized range string or "" when invalid

import { formatDateTimeRange } from "@northguild/gmt/plain/format";
formatDateTimeRange("2024-02-03T09:00:00", "2024-02-03T17:00:00", "en-US", { dateStyle: "long", timeStyle: "short" }) // "February 3, 2024, 9:00 AM - 5:00 PM"
output 

plain/format/formatDateTimeRange.ts