-
Notifications
You must be signed in to change notification settings - Fork 4
Function.formatDateTimeToParts
connor-baer edited this page Dec 5, 2024
·
56 revisions
@sumup-oss/intl / formatDateTimeToParts
formatDateTimeToParts(
date
,locales
?,options
?): (DateTimeFormatPart
| {type
:"date"
;value
:string
; })[]
Formats a Date
to parts with support for various
date
and time styles.
Parameter | Type |
---|---|
date |
FormattableDateTime |
locales ? |
string | string [] |
options ? |
DateTimeFormatOptions |
(DateTimeFormatPart
| {type
: "date"
;value
: string
; })[]
import { formatDateTimeToParts } from '@sumup-oss/intl';
const time = new Date(2000, 1, 1, 9, 55);
formatDateTimeToParts(date, 'de-DE');
// [
// { type: 'day', value: '1' },
// { type: 'literal', value: '.' },
// { type: 'month', value: '2' },
// { type: 'literal', value: '.' },
// { type: 'year', value: '2000' },
// ]
formatDateTimeToParts(date, ['ban', 'id']);
// [
// { type: 'day', value: '1' },
// { type: 'literal', value: '/' },
// { type: 'month', value: '2' },
// { type: 'literal', value: '/' },
// { type: 'year', value: '2000' },
// ]
formatDateTimeToParts(date, 'en-GB', {
year: 'numeric',
month: 'short',
day: 'numeric',
});
// [
// ({ type: 'day', value: '1' },
// { type: 'literal', value: ' ' },
// { type: 'month', value: 'Feb' },
// { type: 'literal', value: ' ' },
// { type: 'year', value: '2000' })
// ]
In runtimes that don't support the Intl.DateTimeFormat.formatToParts
API,
the date is localized and returned as a single string literal part.