formatDateTime
Signature
Section titled “Signature”formatDateTime(value: string, locale?: string | string[], options?: DateTimeFormatOptions): stringReturn 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’sIntl.DateTimeFormat: the requested fields and style widths are kept,eraalone still gets the date and time defaults, andtimeZoneNameis ignored. - Compatibility: before 1.16.0 some locales and calendars lost a requested width, a
long/fulltimeStylereplaced thedateStylewidth ({ dateStyle: "short", timeStyle: "full" }gave"2/3/2024, 2:30:45 PM"),eraalone dropped the time, andtimeZoneNamealone 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.
optionsnull returns"", as ECMA-402’s CoerceOptionsToObject rejects it. Any other non-object — a string, a number, a boolean — formats with the defaults, asIntl.DateTimeFormatdoes: CoerceOptionsToObject calls ToObject on it and the wrapper carries no recognised option. Onlynullandundefinedare special-cased.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value | string | ISO PlainDateTime string |
locale | string | string[] | optional BCP 47 locale identifier (default: runtime default), or a preference list of tags (ECMA-402) |
Options
Section titled “Options”options
optional Intl.DateTimeFormatOptions
Type: DateTimeFormatOptions.
| Option | Type | Default | Description |
|---|---|---|---|
…Intl.DateTimeFormatOptions | — | — | Every field of DateTimeFormatOptions, as ECMA-402 defines it. |
Returns
Section titled “Returns”localized date-time string or "" on invalid input
Related types
Section titled “Related types”Examples
Section titled “Examples”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"