Recipe 11.2
Getting the Parts of a Formatted Date
The formatToParts
method returns an array of tokens representing a formatted date. The resulting array looks like this:
[
{ type: 'month', value: '10' },
{ type: 'literal': value: '/' },
{ type: 'day': value: '1' },
{ type: 'literal', value: '/' },
{ type: 'year', value: '23' }
]
Code
JavaScript
const formatter = new Intl.DateTimeFormat(navigator.language, { dateStyle: 'short' });
const parts = formatter.formatToParts(new Date());