Skip to content

Instantly share code, notes, and snippets.

@brianarn
Created August 29, 2023 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianarn/6c04dbdf75d0d0052d090e318cd9c206 to your computer and use it in GitHub Desktop.
Save brianarn/6c04dbdf75d0d0052d090e318cd9c206 to your computer and use it in GitHub Desktop.
Getting date format
❯ 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
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