Nodejs Date to Json Convert toJSON(key)

Here you can find the source of toJSON(key)

Method Source Code

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  .  j a  v a 2  s. c om

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

Related

  1. toJSON()
    String.format = function () {
        var s = arguments[0];
        for (var i = 0; i < arguments.length - 1; i++) {
            var reg = new RegExp("\\{" + i + "\\}", "gm");
            s = s.replace(reg, arguments[i + 1]);
        return s;
    Date.prototype.toJSON = function () { return String.format("/Date({0})/", this.getTime() - this.getTimezoneOffset() * 60000) }
    ...
    
  2. toJSON()
    Date.prototype.toJSON = function() {
        return this.toISOString();
    };
    
  3. toJSON()
    Date.prototype.toJSON = function() {
      return '"' + this.getUTCFullYear() + '-' +
        (this.getUTCMonth() + 1).toPaddedString(2) + '-' +
        this.getUTCDate().toPaddedString(2) + 'T' +
        this.getUTCHours().toPaddedString(2) + ':' +
        this.getUTCMinutes().toPaddedString(2) + ':' +
        this.getUTCSeconds().toPaddedString(2) + 'Z"';
    };
    
  4. toJSON()
    Date.prototype.toJSON = function() {
        return this.getUTCFullYear() + '/' +
             f(this.getUTCMonth() + 1) + '/' +
             f(this.getUTCDate()) + ' ' +
             f(this.getUTCHours()) + ':' +
             f(this.getUTCMinutes()) + ':' +
             f(this.getUTCSeconds()) + ' +0000';
    };
    
  5. toJSON()
    "use strict";
    Date.prototype.toJSON = function () {
        var year = this.getUTCFullYear(),
            month = this.getUTCMonth() + 1,
            date = this.getUTCDate(),
            hour = this.getUTCHours(),
            minute = this.getUTCMinutes(),
            second = this.getUTCSeconds();
        return (
    ...
    
  6. toJSON(key)
    Date.prototype.toJSON = function(key) {
      function f(n) {
        return n < 10 ? '0' + n : n;
      return isFinite(this.valueOf()) ? this.getFullYear() + '-' + f(this.getMonth() + 1) + '-' + f(this.getDate()) + ' ' + f(this.getHours()) + ':' + f(this.getMinutes()) + ':' + f(this.getSeconds()) : null;
    };
    
  7. toJSON(key)
    Date.prototype.toJSON = function (key) {
        function f(n) {
            return n < 10 ? '0' + n : n;
        return this.getUTCFullYear()   + '-' +
            f(this.getUTCMonth() + 1) + '-' +
            f(this.getUTCDate())      + 'T' +
            f(this.getUTCHours())     + ':' +
            f(this.getUTCMinutes())   + ':' +
    ...
    
  8. toJSON()
    Date.prototype.toJSON = function toJSON() {
      return Date.prototype.toISOString.call(this);
    };
    
  9. toJson()
    Date.prototype.toJson = function() {
      var str = '{';
      str += '"month":' + this.getMonth();
      str += ',"day":' + this.getDay();
      str += ',"year":' + (this.getYear() - 1900);
      str += ',"time":' + this.getTime();
      str += ',"seconds":' + this.getSeconds();
      str += ',"timezoneOffset":' + this.getTimezoneOffset();
      str += ',"date":' + this.getDate();
    ...