Skip to content

Instantly share code, notes, and snippets.

@optinsoft
Last active December 13, 2025 11:18
Show Gist options
  • Select an option

  • Save optinsoft/aa894275f4c238a6bcf1b80a81d7e778 to your computer and use it in GitHub Desktop.

Select an option

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.
/**
* 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