Nodejs Date Convert toISO8601(key)

Here you can find the source of toISO8601(key)

Method Source Code

Date.prototype.toISO8601 = function (key) {
    function f(n) {
        // Format integers to have at least two digits.
        return n < 10 ? '0' + n : n;
    }// ww  w . j av  a 2s .  c om
    function f2(n) {
        // Format integers to have at least three digits.
        if(n < 10)
           return '00' + n;
        return n < 100 ? '0' + n : n;
    }

    return isFinite(this.valueOf()) ?
           this.getUTCFullYear()   + '-' +
         f(this.getUTCMonth() + 1) + '-' +
         f(this.getUTCDate())      + 'T' +
         f(this.getUTCHours())     + ':' +
         f(this.getUTCMinutes())   + ':' +
         f(this.getUTCSeconds())   + '.' +
         f2(this.getUTCMilliseconds()) + '-0000' : null;
};

Related

  1. toEndTimeInputValue(()
    Date.prototype.toEndTimeInputValue = (function() {
      var local = new Date(this);
      local.setMinutes(this.getMinutes() - this.getTimezoneOffset() + 60);
      return local.toJSON().slice(11,13) + ":00:00";
    });
    
  2. toFixedDate()
    Date.prototype.toFixedDate = function(){
      var months = [
        "January", "February", "March",
        "April", "May", "June",
        "Sol",
        "July", "August", "September",
        "October", "November", "December"
      ]
      var daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday",
    ...
    
  3. toFormat(format)
    Date.prototype.toFormat = function (format) {
      format = format.replace(/H/, this.getHours());
      format = format.replace(/m/, this.getMinutes());
      format = format.replace(/s/, this.getSeconds());
      return format;
    };
    
  4. toISO8601()
    Date.prototype.toISO8601 = function() {
      function pad(n) {
        return n < 10 ? '0' + n : n;
      return this.getUTCFullYear() + '-' + pad(this.getUTCMonth() + 1) + '-' + pad(this.getUTCDate()) + 'T' + pad(this.getUTCHours())
       + ':' + pad(this.getUTCMinutes()) + ':' + pad(this.getUTCSeconds()) + 'Z';
    };
    
  5. toISO8601()
    Date.prototype.toISO8601 = function() {    
        var s = [
              this.getFullYear()
            , Date.padZero(this.getMonth() + 1)
            , Date.padZero(this.getDate())
        ].join("-") + "T";
        s += this.getTimeString();
        s += this.getTimezoneOffsetString(true);
        return s;
    ...
    
  6. toInputFormat()
    Date.prototype.toInputFormat = function() {
       var yyyy = this.getFullYear().toString();
       var mm = (this.getMonth()+1).toString(); 
       var dd  = this.getDate().toString();
       return yyyy +'-'+ (mm[1]?mm:"0"+mm[0]) +'-'+ (dd[1]?dd:"0"+dd[0]); 
      };
    
  7. toLocalTime()
    Date.prototype.toLocalTime = function() {
        const newDate = new Date(this.getTime()+this.getTimezoneOffset()*60*1000);
        let offset = this.getTimezoneOffset() / 60;
        const hours = this.getHours();
        if (this.dst()) offset--;
        newDate.setHours(hours - offset);
        return newDate;
    };
    
  8. toLocaleFormat(pattern)
    'use strict';
    Date.prototype.toLocaleFormat = Date.prototype.toLocaleFormat || function (pattern) {
            return pattern.replace(/%Y/g, this.getFullYear()).replace(/%m/g, (this.getMonth() + 1)).replace(/%d/g, this.getDate());
        };
    
  9. toLocaleJSON()
    Date.prototype.toLocaleJSON = function() {
      var t = this,
        y = t.getFullYear(),
        M = t.getMonth() + 1,
        d = t.getDate(),
        h = t.getHours(),
        m = t.getMinutes(),
        s = t.getSeconds();
      return y + '/' + (M < 10 ? '0' + M : M) + '/' + (d < 10 ? '0' + d : d) + ' ' +
    ...