Nodejs Date to Json Convert toJSON()

Here you can find the source of toJSON()

Method Source Code

// -------------------------------------------------------------------------- \\
// File: DateJSON.js                                                          \\
// Module: API                                                                \\
// Author: Neil Jenkins                                                       \\
// License: ? 2010-2015 FastMail Pty Ltd. MIT Licensed.                       \\
// -------------------------------------------------------------------------- \\

"use strict";/*from   w ww .  ja va  2s.c  o m*/

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 (
        ( year < 1000 ?
            '0' + ( year < 100 ? '0' + ( year < 10 ? '0' : '' ) : '' ) + year :
            '' + year ) + '-' +
        ( month < 10 ? '0' + month : '' + month ) + '-' +
        ( date < 10 ? '0' + date : '' + date ) + 'T' +
        ( hour < 10 ? '0' + hour : '' + hour ) + ':' +
        ( minute < 10 ? '0' + minute : '' + minute ) + ':' +
        ( second < 10 ? '0' + second : '' + second )
    );
};

Related

  1. toJSON()
    'use strict';
    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) {
    ...
    
  2. 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) }
    ...
    
  3. toJSON()
    Date.prototype.toJSON = function() {
        return this.toISOString();
    };
    
  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() + '/' +
             f(this.getUTCMonth() + 1) + '/' +
             f(this.getUTCDate()) + ' ' +
             f(this.getUTCHours()) + ':' +
             f(this.getUTCMinutes()) + ':' +
             f(this.getUTCSeconds()) + ' +0000';
    };
    
  6. 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())   + ':' +
    ...
    
  7. 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;
    };
    
  8. 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())   + ':' +
    ...
    
  9. toJSON()
    Date.prototype.toJSON = function toJSON() {
      return Date.prototype.toISOString.call(this);
    };