Variable.formatDateTimeToParts - sumup-oss/intl-js GitHub Wiki
@sumup-oss/intl / formatDateTimeToParts
Variable: formatDateTimeToParts()
constformatDateTimeToParts: (date,locales?,options?) => (DateTimeFormatPart| {type:"date";value:string; })[]
Defined in: lib/date-time-format/index.ts:217
Formats a datetime to parts with support for various date and time styles.
Parameters
| Parameter | Type |
|---|---|
date |
FormattableDateTime |
locales? |
string | string[] |
options? |
DateTimeFormatOptions |
Returns
(DateTimeFormatPart | { type: "date"; value: string; })[]
Example
import { formatDateTimeToParts } from '@sumup-oss/intl';
const date = new Temporal.PlainDateTime(2000, 2, 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' })
// ]
Remarks
In runtimes that don't support the Intl.DateTimeFormat.formatToParts API,
the datetime is localized and returned as a single string literal part.