Nodejs Date to Json Convert toJSON()

Here you can find the source of toJSON()

Method Source Code

'use strict';//from   w  w  w  . ja va 2  s  . c  o m

// jshint ignore:start
function pad(n) {
    return n < 10 ? '0' + n : n;
}

Date.prototype.toJSON = function() {
    var s = this.getFullYear() + '-' + pad(this.getMonth() + 1) + '-' + pad(this.getDate()) + 'T' +
        pad(this.getHours()) + ':' + pad(this.getMinutes()) + ':' + pad(this.getSeconds());
    var offset = this.getTimezoneOffset();
    if (offset === 0) {
        s += 'Z';
    } else {
        s += ( offset < 0 ? '+' : '-' ) + pad(Math.abs(offset / 60)) + pad(Math.abs(offset % 60));
    }
    return s;
};
// jshint ignore:end

Related

  1. toJSON()
    Date.prototype.toJSON = function () {
      return moment(this).format().split("+")[0];
    
  2. toJSON()
    Date.prototype.toJSON = function() {
        var x = new Date(this);
        x.setHours(x.getHours() - x.getTimezoneOffset() / 60);
        return x.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() + '-' +
        (this.getUTCMonth() + 1).toPaddedString(2) + '-' +
        this.getUTCDate().toPaddedString(2) + 'T' +
        this.getUTCHours().toPaddedString(2) + ':' +
        this.getUTCMinutes().toPaddedString(2) + ':' +
        this.getUTCSeconds().toPaddedString(2) + 'Z"';
    };
    
  5. 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"';
    };
    
  6. 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) }
    ...
    
  7. toJSON()
    Date.prototype.toJSON = function() {
        return this.toISOString();
    };
    
  8. 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"';
    };
    
  9. 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';
    };