Added to make dates format to ISO8601 across browsers - Node.js Date

Node.js examples for Date:Date Format

Description

Added to make dates format to ISO8601 across browsers

Demo Code


// h/t: http://stackoverflow.com/a/2218874/124487
Date.prototype.toJSON = function (key) {
    function f(n) {
        // Format integers to have at least two digits.
        return n < 10 ? '0' + n : n;
    }/*from  ww  w . jav  a 2 s .  c  o m*/

    return this.getUTCFullYear()   + '-' +
         f(this.getUTCMonth() + 1) + '-' +
         f(this.getUTCDate())      + 'T' +
         f(this.getUTCHours())     + ':' +
         f(this.getUTCMinutes())   + ':' +
         f(this.getUTCSeconds())   + '.' +
         f(this.getUTCMilliseconds())   + 'Z';
};

Related Tutorials