Skip to content

formatTime

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

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

  • Formats as Temporal’s ECMA-402 PlainTime format (PlainTime#toLocaleString) does, through the runtime’s Intl.DateTimeFormat. era and timeZoneName are ignored, so alone they get the hour, minute and second defaults.
  • Accepts optional BCP 47 locale and Intl.DateTimeFormatOptions.
  • Returns "" for invalid input, for a dateStyle (with or without timeStyle) and for only date fields, where PlainTime#toLocaleString throws a TypeError.
  • Compatibility: before 1.16.0 era or timeZoneName alone returned "", era beside a time field added the era name, and a dateStyle beside timeStyle was ignored. Pass only timeStyle 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 PlainTime string
localestring | string[]optional BCP 47 locale identifier, 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 time string or "" on invalid input

import { formatTime } from "@northguild/gmt/plain/format";
formatTime("14:30:00", "en-US", { timeStyle: "short" }) // "2:30 PM"
output 

plain/format/formatTime.ts