Nodejs Time Calculate timesince(now)

Here you can find the source of timesince(now)

Method Source Code

/* A translation to JavaScript of the Django/Python timesince function:
*  //from www.  j a  va 2 s .c o m
*    https://github.com/django/django/blob/master/django/utils/timesince.py
*/

Date.prototype.timesince = function(now) {
  
  var chunks = [
    [ 60 * 60 * 24 * 365, 'year', 'years'],
    [ 60 * 60 * 24 * 30, 'month', 'months' ],
    [ 60 * 60 * 24, 'day', 'days' ],
    [ 60 * 60, 'hour', 'hours' ],
    [ 60, 'minute', 'minutes' ]
  ];

  now = now || new Date();

  var delta_seconds = Math.floor((now.getTime() - this.getTime())/1000);

  if (delta_seconds <= 0) {
    return '0 minutes';
  }

  for (var i=0; i<chunks.length; i++) {
    count1 = Math.floor( delta_seconds / chunks[i][0] );
    remainder1 = delta_seconds % chunks[i][0];
    if ( count1 != 0 ) {
      break;
    }
  }

  if (i >= chunks.length) {
    return '0 minutes';
  }

  var s = count1 + ' ' + ((count1 > 1) ? chunks[i][2] : chunks[i][1]);

  i++;

  if ( i < chunks.length ) {
    count2 = Math.floor( remainder1 / chunks[i][0] );
    if ( count2 != 0 ) {
      s = s + ', ' + count2 + ' ' + ((count2 > 1) ? chunks[i][2] : chunks[i][1]);
    }
  }

  return s;
}

Date.prototype.timeuntil = function(now) {
  now = now || new Date();
  return now.timesince(this);
}

Related

  1. timeStr()
    var DAY_WIDTH = 100;
    var DAY_HEIGHT = 80;
    var SIDE_DELTA = 2;
    var DAY_HEADING = ["SUN",
                       "MON",
                       "TUE",
                       "WED",
                       "THU",
                       "FRI",
    ...
    
  2. timeToJSON()
    Date.prototype.timeToJSON = function() {
      return this.toLocaleTimeString().substring(0,5);
    };
    
  3. timeToMidnight()
    Date.prototype.timeToMidnight = function() {
        this.setHours(0);
        this.setMinutes(0);
        this.setMilliseconds(0);
    };
    
  4. time_ago_in_words(date)
    function time_between_in_words(from_date, to_date)
      return format_milliseconds(Math.abs(from_date.getTime()-to_date.getTime()));
    function time_ago_in_words(date)
      return format_milliseconds(Math.abs(Date.now()-date.getTime()));
    function format_milliseconds(milsecs)
    ...
    
  5. time_since()
    var chunks = [
      [60 * 60 * 24 * 365, 'year'],
      [60 * 60 * 24 * 30, 'month'],
      [60 * 60 * 24 * 7, 'week'],
      [60 * 60 * 24, 'day'],
      [60 * 60, 'hour'],
      [60, 'minute']
    ];
    Date.prototype.time_since = function() {
    ...
    
  6. withoutTime()
    Date.prototype.withoutTime = function () {
        var d = new Date(this);
        d.setHours(0, 0, 0, 0);
        return d;
    
  7. xsdDateTime()
    Date.prototype.xsdDateTime = function() {
        var yyyy = this.getFullYear().toString();
        var mm = (this.getMonth()+1).toString(); 
        var dd = this.getDate().toString();
        var hh = this.getHours().toString();
        var min = this.getMinutes().toString();
        var sec = this.getSeconds().toString();
        return yyyy + '-' + (mm[1]?mm:"0"+mm[0]) + '-' + (dd[1]?dd:"0"+dd[0]) + 'T' + (hh[1]?hh:"0"+hh[0]) + ':' + (min[1]?min:"0"+min[0]) + ':' + (sec[1]?sec:"0"+sec[0]); 
      };
    ...