Nodejs Time Calculate timeAgoInWords(relativeDate)

Here you can find the source of timeAgoInWords(relativeDate)

Method Source Code

/*//from  w w w . j  av  a2  s  .  com
 *    Copyright (c) 2010 Pete O'Grady 
 *    Freely distributable under the terms of the MIT license.
 */

"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';
   } else if (delta < (45 * 60)) {
       return (parseInt(delta / 60, 10)) + ' minutes ago';
   } else if (delta < (90 * 60)) {
       return 'about an hour ago';
   } else if (delta < (24 * 60 * 60)) {
       return 'about ' + (parseInt(delta / 3600, 10)) + ' hours ago';
   } else if (delta < (48 * 60 * 60)) {
       return '1 day ago';
   } else {
       return (parseInt(delta / 86400, 10)) + ' days ago';
   }
};

Related

  1. 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;
    
  2. time()
    Date.prototype.time = function () {
        return this.getHours().zfill(2) + ":" + this.getMinutes().zfill(2) + ":" + this.getSeconds().zfill(2);
    };
    
  3. 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;
    
  4. 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;
    ...
    
  5. timeAgo(date)
    String.prototype.format = function(obj) {
      var args = arguments;
      var str = this;
      return str.replace(/\{[\w\d_-]+\}/g, function(part) {
        part = part.slice(1, -1);
        var index = parseInt(part, 10);
        if (isNaN(index)) {
          return obj[part];
        } else {
    ...
    
  6. timeFmt(aDate)
    var timeFmt = function (aDate) {
        if (null == aDate) {
            return "";
        else {
            return new Date(aDate).format("yyyy-MM-dd hh:mm:ss");
    };
    
  7. timeSecond()
    Date.prototype.timeSecond = function () {
         return ((this.getSeconds() < 10)?"0":"") + this.getSeconds();
    
  8. 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";
    ...
    
  9. timeStr()
    var DAY_WIDTH = 100;
    var DAY_HEIGHT = 80;
    var SIDE_DELTA = 2;
    var DAY_HEADING = ["SUN",
                       "MON",
                       "TUE",
                       "WED",
                       "THU",
                       "FRI",
    ...