Created
March 17, 2019 12:36
-
-
Save kfiil/b657fa77166154bbf5c07c4b02824567 to your computer and use it in GitHub Desktop.
Javascript add leading zeroes to date
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
// 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