Pad Date value to ISO String - Node.js String

Node.js examples for String:Padding

Description

Pad Date value to ISO String

Demo Code

if (!Date.prototype.toISOString) {
  Date.prototype.toISOString = (function () {

    var pad = function (n, length) {
      length = length || 2
      return (n = n + "", n.length === length) ? n : pad("0" + n, length);
    }//from  w  ww .  j  a va  2  s.  co m

    return function () {
      var year = this.getUTCFullYear()
      year = (year < 0 ? '-' : (year > 9999 ? '+' : '')) + ('00000' + Math.abs(year)).slice(0 <= year && year <= 9999 ? -4 : -6);

      var date = [year, pad(this.getUTCMonth() + 1), pad(this.getUTCDate())].join("-")
      var time = [pad(this.getUTCHours()), pad(this.getUTCMinutes()), pad(this.getUTCSeconds())].join(":") + "." + pad(this.getUTCMilliseconds(), 3)
      return [date, time].join("T") + "Z"
    }
  })()
};

Related Tutorials