Skip to content

Function.formatDate

kodiakhq[bot] edited this page Sep 17, 2024 · 25 revisions

@sumup-oss/intl / formatDate

Function: formatDate()

formatDate(date, locales?, dateStyle?): string

Formats a Date with support for various date styles.

Parameters

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

Returns

string

Example

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

const date = new Date(2000, 1, 1);
const locale = 'en-GB';

formatDate(date, locale, 'short'); // '01/02/2000'
formatDate(date, locale, 'medium'); // '1 Feb 2000'
formatDate(date, locale, 'long'); // '1 February 2000'
formatDate(date, locale, 'full'); // 'Tuesday, 1 February 2000'

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 dateStyle option, the styles are approximated using fallback options.

Defined in

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