Nodejs Time Calculate timeAgo(date)

Here you can find the source of timeAgo(date)

Method Source Code

String.prototype.format = function(obj) {
  var args = arguments;
  var str = this;
  // Support either an object, or a series.
  return str.replace(/\{[\w\d_-]+\}/g, function(part) {
    // Strip off {}.
    part = part.slice(1, -1);/*from ww  w.j av a  2  s .  c  o m*/
    var index = parseInt(part, 10);
    if (isNaN(index)) {
      return obj[part];
    } else {
      return args[index];
    }
  });
};

function formatDate(date) {
  var now = new Date();
  var date_disp;

  date_disp = date.getHours() + ':' + date.getMinutes();
  date_disp += ' - ';

  if (now - date < 24 * 60 * 60 * 1000) {
    date_disp += 'Today';
  } else if (now - date < 48 * 60 * 60 * 1000) {
    date_disp += 'Yesterday';
  } else {
    date_disp += date.getFullYear() + '-';
    var month = date.getMonth() + 1;
    if (month < 10) {
      date_disp += '0';
    }
    date_disp += month + '-' + date.getDate();
  }

  return date_disp;
}

function timeAgo(date) {
  var now = new Date();
  var diff = now - date;

  // These would be displayed as "0 minutes"
  if (diff < 60 * 1000) {
    return "Just now";
  }
  var minutes = diff / 1000 / 60;
  if (minutes < 50) {
    minutes = Math.round(minutes);
    if (minutes == 1) {
      return '1 minute ago';
    } else {
      return minutes + ' minutes ago';
    }
  }
  var hours = minutes / 60;
  if (hours < 22) {
    hours = Math.round(hours);
    if (hours === 1) {
      return '1 hour ago';
    } else {
      return hours + ' hours ago';
    }
  }
  var days = Math.round(hours / 24);
  if (days == 1) {
    return '1 day ago';
  } else {
    return days + ' days ago';
  }
}

Related

  1. prettyDate(time)
    function prettyDate(time) {
        var date = new Date().setISO8601(time || "");
        console.log('date: ' + date);
        var diff = (((new Date()).getTime() - date.getTime()) / 1000);
        console.log('diff: ' + diff);
        var day_diff = Math.floor(diff / 86400);
        console.log('daydiff: ' + day_diff);
        if (isNaN(day_diff) || day_diff < 0 || day_diff >= 31) {
          console.log('some shit went bad');
    ...
    
  2. setTimeToNow()
    Date.prototype.setTimeToNow = function () 
        var n = Date.relativeTo || new Date();
        this.setHours(n.getHours());
        this.setMinutes(n.getMinutes());
        this.setSeconds(n.getSeconds());
        this.setMilliseconds(n.getMilliseconds());
        return this;
    
  3. time()
    Date.prototype.time = function () {
        return this.getHours().zfill(2) + ":" + this.getMinutes().zfill(2) + ":" + this.getSeconds().zfill(2);
    };
    
  4. time()
    Date.prototype.time = function(){
      var hours = this.getHours();
      if (hours === 0 || hours === 24) hours = 12;
      if (hours > 12) hours -= 12;
      var minutes = this.getMinutes();
      if (minutes < 10) minutes = "0" + minutes;
      var ampm = this.getHours() >= 12 ? "pm" : "am";
      return "" + hours + ":" + minutes + ampm;
    
  5. time24()
    Date.prototype.time24 = function()
      var hours = "" + this.getHours();
      var minutes = "" + this.getMinutes();
      var seconds = "" + this.getSeconds();
      if(hours.length < 2)
        hours = "0"+hours;
      if(minutes.length < 2)
        minutes = "0"+minutes;
    ...
    
  6. timeAgoInWords(relativeDate)
    "use strict";
    Date.prototype.timeAgoInWords = function (relativeDate) {
        var delta;
        relativeDate = relativeDate || new Date();
        delta = parseInt((relativeDate.getTime() - this) / 1000, 10);
        if (delta < 60) {
            return 'less than a minute ago';
        } else if (delta < 120) {
          return 'about a minute ago';
    ...
    
  7. timeFmt(aDate)
    var timeFmt = function (aDate) {
        if (null == aDate) {
            return "";
        else {
            return new Date(aDate).format("yyyy-MM-dd hh:mm:ss");
    };
    
  8. timeSecond()
    Date.prototype.timeSecond = function () {
         return ((this.getSeconds() < 10)?"0":"") + this.getSeconds();
    
  9. 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";
    ...