Nodejs Date Ago Format Ago()

Here you can find the source of Ago()

Method Source Code

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)
   {//w w  w.j  ava  2 s .  c om
      return Math.round(elapsed / 1000) + ' seconds ago';
   }

   else if (elapsed < msPerHour)
   {
      return Math.round(elapsed / msPerMinute) + ' minutes ago';
   }

   else if (elapsed < msPerDay)
   {
      return Math.round(elapsed / msPerHour) + ' hours ago';
   }

   else if (elapsed < msPerMonth)
   {
      return '~ ' + Math.round(elapsed / msPerDay) + ' days ago';
   }

   else if (elapsed < msPerYear)
   {
      return '~ ' + Math.round(elapsed / msPerMonth) + ' months ago';
   }

   else
   {
      return '~ ' + Math.round(elapsed / msPerYear) + ' years ago';
   }
};

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 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") ||
    ...
    
  4. 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);
    ...