Nodejs Time Calculate timeSince(date)

Here you can find the source of timeSince(date)

Method Source Code

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";
      }//from w w  w.  j  a v  a  2s .  c o  m
      return "about " + interval + " years ago";
  }
  interval = Math.floor(seconds / 2592000);
  if (interval >= 1) {
      if(interval == 1){
        return "about " + interval + " month ago";
      }
      return "about " + interval + " months ago";
  }
  interval = Math.floor(seconds / 86400);
  if (interval >= 1) {
      if(interval == 1){
        return "about " + interval + " day ago";
      }
      return "about " + interval + " days ago";
  }
  interval = Math.floor(seconds / 3600);
  if (interval >= 1) {
      if(interval == 1){
        return "about " + interval + " hour ago";
      }
      return "about " + interval + " hours ago";
  }
  interval = Math.floor(seconds / 60);
  if (interval >= 1) {
      if(interval == 1){
        return interval + " minute ago";
      }
      return interval + " minutes ago";
  }
  return "less than a minute ago";
};

Related

  1. 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;
    ...
    
  2. 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 {
    ...
    
  3. 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';
    ...
    
  4. timeFmt(aDate)
    var timeFmt = function (aDate) {
        if (null == aDate) {
            return "";
        else {
            return new Date(aDate).format("yyyy-MM-dd hh:mm:ss");
    };
    
  5. timeSecond()
    Date.prototype.timeSecond = function () {
         return ((this.getSeconds() < 10)?"0":"") + this.getSeconds();
    
  6. timeStr()
    var DAY_WIDTH = 100;
    var DAY_HEIGHT = 80;
    var SIDE_DELTA = 2;
    var DAY_HEADING = ["SUN",
                       "MON",
                       "TUE",
                       "WED",
                       "THU",
                       "FRI",
    ...
    
  7. timeToJSON()
    Date.prototype.timeToJSON = function() {
      return this.toLocaleTimeString().substring(0,5);
    };
    
  8. timeToMidnight()
    Date.prototype.timeToMidnight = function() {
        this.setHours(0);
        this.setMinutes(0);
        this.setMilliseconds(0);
    };
    
  9. time_ago_in_words(date)
    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)
    ...