Skip to content

Instantly share code, notes, and snippets.

@kfiil
Created March 17, 2019 12:36
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 kfiil/b657fa77166154bbf5c07c4b02824567 to your computer and use it in GitHub Desktop.
Save kfiil/b657fa77166154bbf5c07c4b02824567 to your computer and use it in GitHub Desktop.
Javascript add leading zeroes to date
// Source: https://stackoverflow.com/a/12550320
function pad(n){return n<10 ? '0'+n : n}
/* use a function for the exact format desired... */
function ISODateString(d){
function pad(n){return n<10 ? '0'+n : n}
return d.getUTCFullYear()+'-'
+ pad(d.getUTCMonth()+1)+'-'
+ pad(d.getUTCDate())+'T'
+ pad(d.getUTCHours())+':'
+ pad(d.getUTCMinutes())+':'
+ pad(d.getUTCSeconds())+'Z'
}
var d = new Date();
console.log(ISODateString(d)); // prints something like 2009-09-28T19:03:12Z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment