Nodejs Date Convert tojson()

Here you can find the source of tojson()

Method Source Code

Date.prototype.tojson = function() {
    var UTC = Date.printAsUTC ? 'UTC' : '';

    var year = this['get'+UTC+'FullYear']().zeroPad(4);
    var month = (this['get'+UTC+'Month']() + 1).zeroPad(2);
    var date = this['get'+UTC+'Date']().zeroPad(2);
    var hour = this['get'+UTC+'Hours']().zeroPad(2);
    var minute = this['get'+UTC+'Minutes']().zeroPad(2);
    var sec = this['get'+UTC+'Seconds']().zeroPad(2);

    if (this['get'+UTC+'Milliseconds']())
        sec += '.' + this['get'+UTC+'Milliseconds']().zeroPad(3);

    var ofs = 'Z';
    if (!Date.printAsUTC) {
        var ofsmin = this.getTimezoneOffset();
        if (ofsmin !== 0){
            ofs = ofsmin > 0 ? '-' : '+'; // This is correct
            ofs += (ofsmin/60).zeroPad(2);
            ofs += (ofsmin%60).zeroPad(2);
        }//from  w w w  .  j  av a  2 s .co  m
    }

    var isodate =  colorize('"' + [year, month, date].join('-') + 'T' + hour +':' + minute + ':' + sec + ofs + '"', "cyan");
    return 'ISODate(' + isodate + ')';
};

Related

  1. toUnixTime()
    Date.prototype.toUnixTime = function() {
        return Math.round(this.getTime() / 1000);
    };
    
  2. toYYYYMMDD()
    Date.prototype.toYYYYMMDD = function() {
        return this.getFullYear() + String(this.getMonth()+1).lpad(2, "0") + String(this.getDate()).lpad(2, "0");
    
  3. toYmdHis()
    Date.prototype.toYmdHis = function() {
        return this.getFullYear() + "-" + twoDigits(1 + this.getMonth()) + "-" + twoDigits(this.getDate()) + " " + twoDigits(this.getHours()) + ":" + twoDigits(this.getMinutes()) + ":" + twoDigits(this.getSeconds());
    };
    
  4. to_DMY()
    Date.prototype.to_DMY = function(){
        return Util.leadingzero(this.getDate()) + '/' + Util.leadingzero(this.getMonth() + 1) + '/' + this.getFullYear()
    };
    
  5. to_datetimepicker_format()
    Date.prototype.to_datetimepicker_format = function() {
        var Y = this.getUTCFullYear();
        var M = this.getUTCMonth() + 1;
        var D = this.getUTCDate();
        var h = this.getUTCHours();
        var m = this.getUTCMinutes();
        return Y + "-" + M.pad() + "-" + D.pad() + " " + h.pad() + ":" + m.pad();
    
  6. Date2Julian()
    var timer = function(){
      this.count = 1;
      this.multiplier = .25;
      return this;
    Date.prototype.Date2Julian = function() {
      return Math.floor( ( this / 86400000 ) - ( this.getTimezoneOffset() / 1440 ) + 2440587.5 );
    };
    Number.prototype.Julian2Date = function() {
    ...