Nodejs Distance Calculate distanceOfTimeInWords(from, to)

Here you can find the source of distanceOfTimeInWords(from, to)

Method Source Code

// This is a manifest file that'll be compiled into including all the files listed below.
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
// be included in the compiled file accessible from http://example.com/assets/application.js
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
////  w  ww.ja  v  a2  s  . c o  m
//= require jquery
//= require jquery_ujs
//= require_tree .

window.distanceOfTimeInWords = function (from, to) {
  var distance_in_milliseconds = to - from;
  var distance_in_minutes = Math.round(Math.abs(distance_in_milliseconds / 60000));
  var words = "";
  if (distance_in_minutes == 0) {
    words = "less than a minute";
  } else if (distance_in_minutes == 1) {
    words = "1 minute";
  } else if (distance_in_minutes < 45) {
    words = distance_in_minutes + " minutes";
  } else if (distance_in_minutes < 90) {
    words = "about 1 hour";
  } else if (distance_in_minutes < 1440) {
    words = "about " + Math.round(distance_in_minutes / 60) + " hours";
  } else if (distance_in_minutes < 2160) {
    words = "about 1 day";
  } else if (distance_in_minutes < 43200) {
    words = Math.round(distance_in_minutes / 1440) + " days";
  } else if (distance_in_minutes < 86400) {
    words = "about 1 month";
  } else if (distance_in_minutes < 525600) {
    words = Math.round(distance_in_minutes / 43200) + " months";
  } else if (distance_in_minutes < 1051200) {
    words = "about 1 year";
  } else {
    words = "over " + Math.round(distance_in_minutes / 525600) + " years";
  }

  return words;
};

Date.prototype.timeAgoInWords = function() {
  return distanceOfTimeInWords(this, new Date());
};

Date.prototype.untilInWords = function() {
  return distanceOfTimeInWords(new Date(), this);
};

Related

  1. distance(lat1, lon1, lat2, lon2)
    function distance(lat1, lon1, lat2, lon2) {
      var R = 6371;
      var a = 0.5 - Math.cos((lat2 - lat1) * Math.PI / 180)/2 + 
         Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) * 
         (1 - Math.cos((lon2 - lon1) * Math.PI / 180))/2;
      var m = R * 2 * Math.asin(Math.sqrt(a))*1000;
      return Math.floor(m);
    
  2. distance(lat1,lon1,lat2,lon2)
    function distance(lat1,lon1,lat2,lon2) {
      var R = 6371; 
      var dLat = deg2rad(lat2-lat1);  
      var dLon = deg2rad(lon2-lon1);
      var a =
        Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
        Math.sin(dLon/2) * Math.sin(dLon/2)
        ;
    ...
    
  3. distance(p1, p2)
    function Point(x, y) {
        this.x = x;
        this.y = y;
    function randomPoint() {
        var randomx = randomNumber(WIDTH);
        var randomy = randomNumber(HEIGHT);
        var randomPoint = new Point(randomx, randomy);
        return randomPoint;
    ...
    
  4. distance(x1, x2, y1, y2)
    Math.distance = function(x1, x2, y1, y2){
      var x = x2 - x1;
      var y = y2 - y1;
      return Math.sqrt(x * x + y * y);
    };
    
  5. distanceBetweenTwoPoints(p1, p2)
    Util.distanceBetweenTwoPoints = function(p1, p2) {
      return Math.sqrt(Math.pow(p2.y - p1.y, 2) + Math.pow(p2.x - p1.x, 2)).toFixed(2);
    };
    
  6. getEditDistance(a, b)
    function getEditDistance(a, b){
      if(a.length == 0) return b.length;
      if(b.length == 0) return a.length;
      var matrix = [];
      var i;
      for(i = 0; i <= b.length; i++){
        matrix[i] = [i];
      var j;
    ...
    
  7. get_distance (lat1, lon1, lat2, lon2)
    function get_distance (lat1, lon1, lat2, lon2) {
      var R = 6371;
      var dLat = deg2rad(lat2-lat1);
      var dLon = deg2rad(lon2-lon1);
      var lat1 = deg2rad(lat1);
      var lat2 = deg2rad(lat2);
      var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
              Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
      var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    ...
    
  8. haversineDistance(lat1, lng1, lat2, lng2)
    var haversineDistance(lat1, lng1, lat2, lng2) {
        var R = 6371; 
        var deltaLat = (lat2-lat1).inRadians();
        var deltaLng = (lng2-lng1).inRadians();
        var a = (Math.sin(deltaLat/2) * Math.sin(deltaLat/2)) + (Math.cos(lat1.inRadians()) * Math.cos(lat2.inRadians()) *
                       Math.sin(deltaLng/2) * Math.sin(deltaLng/2));
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        return R * c; 
    
  9. haversineDistance(lat1, lon1, lat2, lon2)
    function haversineDistance(lat1, lon1, lat2, lon2){
      var R = 6371; 
      var x1 = lat2-lat1;
      var dLat = x1.toRad();  
      var x2 = lon2-lon1;
      var dLon = x2.toRad();  
      var a = Math.sin(dLat/2) * Math.sin(dLat/2) + 
      Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
      Math.sin(dLon/2) * Math.sin(dLon/2);  
    ...