Created
January 29, 2024 11:01
-
-
Save sylt/4bc1eb733d0c2d4e3198f02eeb3ca048 to your computer and use it in GitHub Desktop.
Date as YYYY-MM-DD HH:MM:SS.sss as local time (ISO 8601)
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
function prettyDate(date) { | |
const dateStr = [`${date.getFullYear()}`, `${date.getMonth() + 1}`, `${date.getDate()}`] | |
.map((part) => part.padStart(2, '0')) | |
.join('-'); | |
const timeStr = [`${date.getHours()}`, `${date.getMinutes()}`, `${date.getSeconds()}`] | |
.map((part) => part.padStart(2, '0')) | |
.join(':'); | |
const fractionStr = `${date.getMilliseconds()}`.padStart(3, '0'); | |
return `${dateStr} ${timeStr}.${fractionStr}`; | |
} | |
// Example usage of printing the current date/time: | |
console.log(prettyDate(new Date())); | |
// Try fiddle with date yourself (printed differently depending on your time zone): | |
const parsedDate = Date.parse("2023-12-31T23:01:02.003Z"); | |
console.log(prettyDate(new Date(parsedDate))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment