Skip to content

Function.formatTime

kodiakhq[bot] edited this page Dec 2, 2024 · 56 revisions

@sumup-oss/intl / formatTime

Function: formatTime()

formatTime(date, locales?, timeStyle?): string

Formats a Date with support for various time styles.

Parameters

Parameter Type Default value
date FormattableTime undefined
locales? string | string[] undefined
timeStyle? undefined | "short" | "long" | "full" | "medium" 'short'

Returns

string

Example

import { formatTime } from '@sumup-oss/intl';

const time = new Date(2000, 1, 1, 9, 55);
const locale = 'en-GB';

formatTime(time, locale, 'short'); // '09:55'
formatTime(time, locale, 'medium'); // '09:55:00'
formatTime(time, locale, 'long'); // '09:55:00 CET'
formatTime(time, locale, 'full'); // '09:55:00 Central European Standard Time'

Remarks

In runtimes that don't support the Intl.DateTimeFormat API, the date is formatted using the Date.toLocale(Date|Time)String API.

In runtimes that don't support the timeStyle option, the styles are approximated using fallback options.

Defined in

lib/date-time-format/index.ts:118