Nodejs Timestamp Calculate getTimestamp()

Here you can find the source of getTimestamp()

Method Source Code

function fixNumber(number, scale) {
   if (typeof(scale) === 'undefined') {
      scale = 2;//from   w w w  .  j  a va  2 s .  co  m
   }

   var n = number;
   var str = '';

   for (var i=scale; i>=1; i--) {
      var p = Math.pow(10, i-1);

      if (n >= p) {
         str += Math.floor(n / p);
      } else {
         str += '0';
      }

      n = n % p;
   }

   return str;
}

Date.prototype.getTimestamp = function() {
  var timestamp = this.getFullYear() + '-' + fixNumber(this.getMonth() + 1) + '-' + fixNumber(this.getDate());
  timestamp += ' ' + this.toTimeString().substring(0, 8);

  return timestamp;
};

Related

  1. getDateTimeStamp()
    Date.prototype.getDateTimeStamp = function() {
      let hours = this.getHours();
      let minutes = this.getMinutes();
      let seconds = this.getSeconds();
      return this.getDateStamp() + '-' + [
        (hours > 9 ? '' : '0') + hours,
        (minutes > 9 ? '' : '0') + minutes,
        (seconds > 9 ? '' : '0') + seconds
      ].join('');
    ...
    
  2. toH2Timestamp()
    Date.prototype.toH2Timestamp = function(){
      return [this.getFullYear(), '-',
          this.pad(this.getMonth()+1), '-',
          this.pad(this.getDate()), ' ',
          this.pad(this.getHours()), ':',
          this.pad(this.getMinutes()), ':',
          this.pad(this.getSeconds()) ].join('');
    
  3. toUnixTimestamp()
    Date.prototype.toUnixTimestamp = function() { 
      return this.getTime()/1000 
    };