brianonn
1/2/2016 - 3:58 PM

JS: create an ISO8601 date string

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);
};