Last active
December 13, 2025 11:18
-
-
Save optinsoft/aa894275f4c238a6bcf1b80a81d7e778 to your computer and use it in GitHub Desktop.
Formats a JavaScript Date into a local timestamp string: YYYY-MM-DDTHH:MM:SS.mmm.
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
| /** | |
| * Formats a Date object into a string of the form: | |
| * YYYY-MM-DDTHH:MM:SS.mmm | |
| * | |
| * All numeric components are zero-padded to ensure fixed width. | |
| * The result reflects the local time of the supplied Date object | |
| * and does not include any timezone suffix. | |
| * | |
| * @param {Date} dt - The Date object to format. | |
| * @returns {string} The formatted timestamp string. | |
| */ | |
| function formatDate(dt) { | |
| const pad = (n) => String(n).padStart(2, '0'); | |
| const ms = String(dt.getMilliseconds()).padStart(3, '0'); | |
| return `${dt.getFullYear()}-${pad(dt.getMonth() + 1)}-${pad(dt.getDate())}` + | |
| `T${pad(dt.getHours())}:${pad(dt.getMinutes())}:${pad(dt.getSeconds())}.${ms}`; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment