Last active
January 31, 2017 05:10
-
-
Save brianonn/292d3cb472e62480ac7f to your computer and use it in GitHub Desktop.
JS: create an ISO8601 date string
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
//credits: http://stackoverflow.com/questions/2573521/how-do-i-output-an-iso-8601-formatted-string-in-javascript | |
// params: d - a javascript Date object | |
// example: sTimestamp = ISODateString(new Date) | |
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'; | |
} | |
// can be added to the Date prototype | |
Date.prototype.toISOShortString = function() { | |
return ISODateString(this); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment