Javascript Date toJSON() method

Description

Javascript Date toJSON() method


Date.prototype.toJSON = function() {
  return moment(this).format('YYYY-MM-DD');
};

Javascript Date toJSON()

Date.prototype.toJSON = function () {
  return moment(this).format().split("+")[0];
}

Javascript Date toJSON()

Date.prototype.toJSON = function () {
  return this.getTime();
};

Javascript Date toJSON()

/**//from   ww  w  . ja v  a  2s. co m
 * Override default toJSON of Date.
 * 
 * @returns {String}
 */
Date.prototype.toJSON = function() {
  var f = function(v) {
    if (v >= 10)
      return v;
    return "0" + v;
  }
  var s = "-", y = this.getFullYear(), m = f(this.getMonth() + 1), d = f(this.getDate());
  return y + s + m + s + d;
}

Javascript Date toJSON()

// this changes JSON-serialization for dates, 
// because we usually want the time to be the same across time zones and NOT keeping the same moment
Date.prototype.toJSON = function() {
    var x = new Date(this);
    x.setHours(x.getHours() - x.getTimezoneOffset() / 60);
    return x.toISOString();
};

Javascript Date 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"';
};

Javascript Date toJSON()

'use strict';//from  ww  w  .  j  a v  a  2  s.co  m

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

/* eslint-disable no-extend-native */
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;
};
/* eslint-enable no-extend-native */

Javascript Date toJSON()

/** section: Language/*ww  w  .ja va2s  .c o m*/
 * class Date
 *  
 *  Extensions to the built-in `Date` object.
**/

/**
 *  Date#toJSON() -> String
 *  
 *  Produces a string representation of the date in ISO 8601 format.
**/
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"';
};

Javascript Date toJSON()

Date.prototype.toJSON = function() {
    return this.toISOString();
};

Javascript Date toJSON()

'use strict';//from   www  .j  av a2s .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

Javascript Date toJSON()

//Date.prototype.toJSON = function() { return "{timestamp}+" . this.getTime() }
Date.prototype.toJSON = function () { return String.format("/Date({0})/", this.getTime() - this.getTimezoneOffset() * 60000) }

function customJSONstringify(obj) {
    return JSON.stringify(obj).replace(/\/Date/g, "\\\/Date").replace(/\)\//g, "\)\\\/")
}

Javascript Date toJSON()

/** section: Language/*  w  w w.  j a  v  a 2 s  .c  om*/
 * class Date
 *
 *  Extensions to the built-in `Date` object.
**/

/**
 *  Date#toJSON() -> String
 *
 *  Produces a string representation of the date in ISO 8601 format.
 *  The time zone is always UTC, as denoted by the suffix "Z".
 *
 *  <h5>Example</h5>
 *
 *      var d = new Date(1969, 11, 31, 19);
 *      d.getTimezoneOffset();
 *      //-> -180 (time offest is given in minutes.)
 *      d.toJSON();
 *      //-> '"1969-12-31T16:00:00Z"'
**/
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"';
};



PreviousNext

Related