Skip to content

formatDateTime

formatDateTime(value: string, locale?: string | string[], options?: DateTimeFormatOptions): string

Return a localized string for a PlainDateTime ISO input using Intl options.

  • Formats as Temporal’s ECMA-402 PlainDateTime format (PlainDateTime#toLocaleString) does, through the runtime’s Intl.DateTimeFormat: 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 and calendars lost a requested width, a long/full timeStyle replaced the dateStyle width ({ dateStyle: "short", timeStyle: "full" } gave "2/3/2024, 2:30:45 PM"), era alone dropped the time, and timeZoneName alone returned "". Pass the fields the old text showed to keep it.
  • Accepts optional BCP 47 locale and Intl.DateTimeFormatOptions.
  • Returns "" for invalid input.
  • 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
valuestringISO PlainDateTime string
localestring | string[]optional BCP 47 locale identifier (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 date-time string or "" on invalid input

import { formatDateTime } from "@northguild/gmt/plain/format";
formatDateTime("2024-03-15T14:30:00", "en-US", { dateStyle: "medium", timeStyle: "short" }) // "Mar 15, 2024, 2:30 PM"
output 

plain/format/formatDateTime.ts