Nodejs Date Ago Format ago()

Here you can find the source of ago()

Method Source Code

Date.prototype.ago = function() {
   var diff = (((new Date()).getTime() - this.getTime()) / 1000)
   , day_diff = Math.floor(diff / 86400);

   return day_diff == 0 && (
      diff < 60 && "just now" ||
      diff < 120 && "1 minute ago" ||
      diff < 3600 && Math.floor( diff/60 ) + " minutes ago" ||
      diff < 7200 && "1 hour ago" ||
      diff < 86400 && Math.floor( diff/3600 ) + " hours ago") ||
      day_diff == 1 && "Yesterday" ||
      day_diff < 7 && day_diff + " days ago" ||
      Math.ceil( day_diff/7 ) + " weeks ago";
});//from   ww  w.j  a  va  2 s  . co m

Related

  1. ago()
    Number.prototype.ago = function() {
      var d = new Date();
      return (d.getTime() - this); 
    };
    
  2. ago()
    Number.prototype.ago = function () {
      var i = this.valueOf();
      var t = new Date().getTime();
      return new Date(t - i);
    };
    
  3. ago()
    Date.prototype.ago = function () {
      var now = new Date();
      var seconds_ago = (now.getTime() - this.getTime()) / 1000;
      var time = Math.floor(seconds_ago / 86400);
      var s = (time == 1) ? "" : "s";
      if (seconds_ago < 60) {
        return "less than a minute ago";
      } else if (seconds_ago < 3600) {
        time = Math.round(seconds_ago / 60);
    ...
    
  4. Ago()
    Date.prototype.Ago = function ()
      var msPerMinute = 60 * 1000;
      var msPerHour = msPerMinute * 60;
      var msPerDay = msPerHour * 24;
      var msPerMonth = msPerDay * 30;
      var msPerYear = msPerDay * 365;
      var elapsed = new Date() - this;
      if (elapsed < msPerMinute)
    ...