-
-
Save abozhilov/95e6f06296a8593dd819d75b20166a74 to your computer and use it in GitHub Desktop.
Intl.DateTimeFormat
This file contains hidden or 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
function format(date: Date) { | |
const formatter = new Intl.DateTimeFormat('en-US', { | |
year: 'numeric', | |
month: '2-digit', | |
day: '2-digit', | |
hour: '2-digit', | |
minute: '2-digit' | |
}); | |
const mapParts = Object.create(null); | |
const parts = formatter.formatToParts(date); | |
for (const {type, value} of parts) { | |
mapParts[type] = value; | |
} | |
return `${mapParts.year}-${mapParts.month}-${mapParts.day} ${mapParts.hour}:${mapParts.minute}`; | |
} | |
const date = new Date(); | |
const start = new Date(); | |
for (let i = 0; i < 100000; i++) { | |
format(date); | |
} | |
const end = new Date(); | |
console.log(format(date)); | |
console.log(end.getTime() - start.getTime()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment