Skip to content

Instantly share code, notes, and snippets.

@brianonn
Last active January 31, 2017 05:10
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 brianonn/292d3cb472e62480ac7f to your computer and use it in GitHub Desktop.
Save brianonn/292d3cb472e62480ac7f to your computer and use it in GitHub Desktop.
JS: create an ISO8601 date string
//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