Created
August 29, 2023 23:32
-
-
Save brianarn/6c04dbdf75d0d0052d090e318cd9c206 to your computer and use it in GitHub Desktop.
Getting date format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
❯ node dateformat.js | |
The date: 2023-08-29T23:30:01.758Z | |
Formatted array: [ | |
{ type: 'month', value: '8' }, | |
{ type: 'literal', value: '/' }, | |
{ type: 'day', value: '29' }, | |
{ type: 'literal', value: '/' }, | |
{ type: 'year', value: '2023' }, | |
{ type: 'literal', value: ', ' }, | |
{ type: 'timeZoneName', value: 'MDT' } | |
] | |
Formatted: MDT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const someDate = new Date(); | |
console.log('The date:', someDate); | |
const formatter = new Intl.DateTimeFormat([], { timeZoneName: 'short' }); | |
const formattedArray = formatter.formatToParts(someDate); | |
console.log('Formatted array:', formattedArray); | |
const formattedValue = formattedArray.find(part => part.type === 'timeZoneName')?.value; | |
console.log('Formatted:', formattedValue); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment