Nodejs Date to Json Convert toJson()

Here you can find the source of toJson()

Method Source Code

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();
  str += ',"hours":' + this.getHours();
  str += ',"minutes":' + this.getMinutes();
  str += '}';/*  www.j a v  a  2 s  .c  om*/
  return str;
}

Related

  1. 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 (
    ...
    
  2. 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())   + ':' +
    ...
    
  3. 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;
    };
    
  4. 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())   + ':' +
    ...
    
  5. toJSON()
    Date.prototype.toJSON = function toJSON() {
      return Date.prototype.toISOString.call(this);
    };
    
  6. toJson(key)
    Date.prototype.toJson = function(key){
      return isFinite(this.valueOf()) ?
        this.getUTCFullYear().toFormattedString(2, 0) + '-' +
          (this.getUTCMonth() + 1).toFormattedString(2, 0) + '-' +
          this.getUTCDate().toFormattedString(2, 0) + 'T' +
          this.getUTCHours().toFormattedString(2, 0) + ':' +
          this.getUTCMinutes().toFormattedString(2, 0) + ':' +
          this.getUTCSeconds().toFormattedString(2, 0) + 'Z'
        : null;
    ...