Nodejs Date ISO Format toISOString()

Here you can find the source of toISOString()

Method Source Code

/**// w w w .  j ava  2s.co  m
 * Date.prototype.toISOString Polyfill
 *
 * This function taken with minor changes from https://github.com/kriskowal/es5-shim
 */

Date.prototype.toISOString = function toISOString() {
   var result, length, value;
   if (! isFinite(this)) {
      throw new RangeError();
   }

   // the date time string format is specified in 15.9.1.15.
   result = [this.getUTCFullYear(), this.getUTCMonth() + 1, this.getUTCDate(),
      this.getUTCHours(), this.getUTCMinutes(), this.getUTCSeconds()];

   length = result.length;
   while (length--) {
      value = result[length];
      // pad months, days, hours, minutes, and seconds to have two digits.
      if (value < 10) {
         result[length] = '0' + value;
      }
   }
   // pad milliseconds to have three digits.
   return result.slice(0, 3).join('-') + 'T' + result.slice(3).join(':') + '.' +
      ('000' + this.getUTCMilliseconds()).slice(-3) + 'Z';
};

/* End of file toisostring.js */

Related

  1. toISOString()
    function OzeroPad(val) {
      return val < 10 ? '0' + val : '' + val;
    if(!Date.prototype.toISOString) {
      Date.prototype.toISOString = function () {
        var YYYY = this.getUTCFullYear(),
        MM = OzeroPad(this.getUTCMonth() + 1),
        DD = OzeroPad(this.getUTCDate()),
        HH = OzeroPad(this.getUTCHours()),
    ...
    
  2. toISOString()
    Date.prototype.toISOString = Date.prototype.toISOString || function() {
      return this.getUTCFullYear() + "-"
        + ("0" + this.getUTCMonth() + 1 + "-").slice(-3)
        + ("0" + this.getUTCDate() + "T").slice(-3)
        + ("0" + this.getUTCHours() + ":").slice(-3)
        + ("0" + this.getUTCMinutes() + ":").slice(-3)
        + ("0" + this.getUTCSeconds() + ".").slice(-3)
        + ("00" + this.getUTCMilliseconds() + "Z").slice(-4);
    };
    ...
    
  3. toISOString(withMilliSeconds)
    function pad(number) {
      if (number < 10) {
        return '0' + number;
      return number;
    Date.prototype.toISOString = function(withMilliSeconds) {
      var ISOString = this.getUTCFullYear() +
        '-' + pad(this.getUTCMonth() + 1) +
    ...
    
  4. toISOString()
    Date.prototype.toISOString = function toISOString() {
      var date = this;
      function pad(str, len) {
        var pad = "0000";
        str = '' + str;
        return pad.substr(0, len - str.length) + str
      var y = date.getUTCFullYear(),
      m = pad(date.getUTCMonth() + 1, 2),
    ...
    
  5. toISOString()
    var global = (function () { return this; }).call(null);
    function pad(number) {
      if (number < 10) {
        return '0' + number;
      return number;
    Date.prototype.toISOString = function toISOString() {
      return this.getUTCFullYear() +
    ...
    
  6. setISO8601(dString)
    Date.prototype.setISO8601 = function(dString) {
      var regexp = /(\d\d\d\d)(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|([+-])(\d\d)(:)?(\d\d))/;
      if (dString.toString().match(new RegExp(regexp))) {
        var d = dString.match(new RegExp(regexp));
        var offset = 0;
        this.setUTCDate(1);
        this.setUTCFullYear(parseInt(d[1], 10));
        this.setUTCMonth(parseInt(d[3], 10) - 1);
        this.setUTCDate(parseInt(d[5], 10));
    ...
    
  7. setISO8601(dString)
    Date.prototype.setISO8601 = function(dString){
        var regexp = /(\d\d\d\d)(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|([+-])(\d\d)(:)?(\d\d))/;
        if (dString.toString().match(new RegExp(regexp))) {
            var d = dString.match(new RegExp(regexp));
            var offset = 0;
            this.setUTCDate(1);
            this.setUTCFullYear(parseInt(d[1],10));
            this.setUTCMonth(parseInt(d[3],10) - 1);
            this.setUTCDate(parseInt(d[5],10));
    ...
    
  8. setISO8601(dString)
    Date.prototype.setISO8601 = function(dString){
      var regexp = /(\d\d\d\d)(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|([+-])(\d\d)(:)?(\d\d))/;
      var d = dString.toString().match(regexp);
      if (d) {
        var offset = 0;
        this.setUTCDate(1);
        this.setUTCFullYear(parseInt(d[1],10));
        this.setUTCMonth(parseInt(d[3],10) - 1);
        this.setUTCDate(parseInt(d[5],10));
    ...
    
  9. setISO8601(dString)
    Date.prototype.setISO8601 = function(dString) {
        var regexp = /(\d\d\d\d)(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|([+-])(\d\d)(:)?(\d\d))/;
        if (dString.toString().match(new RegExp(regexp))) {
            var d = dString.match(new RegExp(regexp));
            var offset = 0;
            this.setUTCDate(1);
            this.setUTCFullYear(parseInt(d[1], 10));
            this.setUTCMonth(parseInt(d[3], 10) - 1);
            this.setUTCDate(parseInt(d[5], 10));
    ...