@northguild/gmt
Version:
Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.
33 lines • 2.23 kB
TypeScript
import { DateTimeFormatOptions } from "../../types/datetime-format-options.js";
import "../../types/index.js";
/**
* 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.
*
* @param value ISO PlainTime string
* @param locale optional BCP 47 locale identifier, or a preference list of tags (ECMA-402)
* @param options optional Intl.DateTimeFormatOptions
* @returns localized time string or "" on invalid input
*
* @example formatTime("14:30:00", "hi-IN", { timeStyle: "short" }) // "2:30 pm"
* @example formatTime("14:30:00", "de-DE", { timeStyle: "short" }) // "14:30"
* @example formatTime("14:30:45", "pt-BR", { era: "long" }) // "14:30:45"
* @example formatTime("14:30:45", "ja-JP", { dateStyle: "short", timeStyle: "short" }) // "" — a PlainTime has no date
* @example formatTime("14:30:45", "ja-JP", { timeStyle: "short" }) // "14:30" — the pre-1.16.0 text
* @example formatTime("invalid") // ""
* @example formatTime("14:30:45", ["fr-FR", "en-US"], { hour: "2-digit", minute: "2-digit" }) // "14:30"
*/
export declare function formatTime(value: string, locale?: string | string[], options?: DateTimeFormatOptions): string;