Nodejs Time Calculate time_ago_in_words(date)

Here you can find the source of time_ago_in_words(date)

Method Source Code

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)
{
  var one_second = 1000;
  var one_minute = one_second*60;
  var one_hour = one_minute*60;
  var one_day = one_hour*24;
  var one_month = one_day*30;
  var one_year = one_day * 365;

  if (milsecs < one_second)
    return "a moment";
  if (milsecs >= one_second && milsecs < one_minute){
    var value = Math.floor(milsecs/one_second);
    return format_plural(value, value + " second");
  }/*w ww.j  a  v  a 2s . c o m*/
  if (milsecs >= one_minute && milsecs < one_hour){
    var value = Math.floor(milsecs/one_minute);
    return format_plural(value, value + " minute");
  }
  if (milsecs >= one_hour && milsecs < one_day){
    var value = Math.floor(milsecs/one_hour);
    return format_plural(value, value + " hour");
  }
  if (milsecs >= one_day && milsecs < one_month){
    var value = Math.floor(milsecs/one_day);
    return format_plural(value, value + " day");
  }
  if (milsecs >= one_month && milsecs < one_year){
    var value = Math.floor(milsecs/one_month);
    return format_plural(value, value + " month");
  }
  if (milsecs >= one_year){
    var value = Math.floor(milsecs/one_year);
    return format_plural(value, value + " year");
  }
}

function format_plural(value, string)
{
  if (value != 1)
    string += "s";
  return string;
}

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

Date.prototype.time_between_in_words = function(to_date) {
  return time_between_in_words(this, to_date);
};


module.exports.time_between_in_words = time_between_in_words;
module.exports.time_ago_in_words = time_ago_in_words;

Related

  1. timeSecond()
    Date.prototype.timeSecond = function () {
         return ((this.getSeconds() < 10)?"0":"") + this.getSeconds();
    
  2. timeSince(date)
    String.prototype.startsWith = function (str){
      return this.indexOf(str) === 0;
    };
    timeSince = function (date) {
      var seconds = Math.floor((new Date() - date) / 1000);
      var interval = Math.floor(seconds / 31536000);
      if (interval >= 1) {
          if(interval == 1){
            return "about " + interval + " year ago";
    ...
    
  3. timeStr()
    var DAY_WIDTH = 100;
    var DAY_HEIGHT = 80;
    var SIDE_DELTA = 2;
    var DAY_HEADING = ["SUN",
                       "MON",
                       "TUE",
                       "WED",
                       "THU",
                       "FRI",
    ...
    
  4. timeToJSON()
    Date.prototype.timeToJSON = function() {
      return this.toLocaleTimeString().substring(0,5);
    };
    
  5. timeToMidnight()
    Date.prototype.timeToMidnight = function() {
        this.setHours(0);
        this.setMinutes(0);
        this.setMilliseconds(0);
    };
    
  6. 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() {
    ...
    
  7. timesince(now)
    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();
    ...
    
  8. withoutTime()
    Date.prototype.withoutTime = function () {
        var d = new Date(this);
        d.setHours(0, 0, 0, 0);
        return d;
    
  9. 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]); 
      };
    ...