getDateFormatter() function

Returns a date formatter function for the given locales and date skeleton

Signature:

export declare function getDateFormatter(locales: string | string[], tokens: string | DateToken[], timeZone?: string | ((error: DateFormatError) => void), onError?: (error: DateFormatError) => void): (date: Date | number) => string;

Parameters

Parameter Type Description
locales string | string[] One or more valid BCP 47 language tags, e.g. fr or en-CA
tokens string | DateToken[] An ICU DateFormat skeleton string, or an array or parsed DateToken tokens
timeZone string | ((error: DateFormatError) => void) (Optional)
onError (error: DateFormatError) => void (Optional) If defined, will be called separately for each encountered parsing error and unsupported feature.

Returns:

(date: Date | number) => string

Remarks

Uses Intl.DateTimeFormat internally.

Example

import { getDateFormatter } from '@messageformat/date-skeleton'

// 2006 Jan 2, 15:04:05.789 in local time
const date = new Date(2006, 0, 2, 15, 4, 5, 789)

let fmt = getDateFormatter('en-CA', 'GrMMMdd', console.error)
fmt(date) // 'Jan. 02, 2006 AD'

fmt = getDateFormatter('en-CA', 'hamszzzz', console.error)
fmt(date) // '3:04:05 p.m. Newfoundland Daylight Time'