Skip to content

formatDate

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

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

  • Formats as Temporal’s ECMA-402 PlainDate format (PlainDate#toLocaleString) does, through the runtime’s Intl.DateTimeFormat: the requested fields and widths are kept, and timeZoneName is ignored (a PlainDate has no zone).
  • Accepts optional BCP 47 locale and Intl.DateTimeFormatOptions.
  • Returns "" for invalid input, for a timeStyle (with or without dateStyle) and for only time fields, where PlainDate#toLocaleString throws a TypeError.
  • Compatibility: before 1.16.0 some locales and calendars lost a requested width (ja-JP with the japanese calendar and month: "long" gave "R6/2"), timeZoneName alone returned "", and a timeStyle beside dateStyle was ignored. Pass the numeric fields to keep the numeric text, and pass only dateStyle to keep the styled text.
  • 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 PlainDate 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 string or "" on invalid input

import { formatDate } from "@northguild/gmt/plain/format";
formatDate("2024-03-15", "en-US", { year: "numeric", month: "long", day: "numeric" }) // "March 15, 2024"
output 

plain/format/formatDate.ts