Sha256: 7df03491c1194f9b42698b2e81adc9850dab2ec34874170cde4e1cfcd201ad36

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

// Thanks, http://www.harukizaemon.com/2008/05/javascript-date-helpers.html
Date.prototype.distanceOfTimeInWords = function(to) {
  distance_in_milliseconds = to - this;

  distance_in_minutes = Math.round(Math.abs(distance_in_milliseconds / 60000));

  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 this.distanceOfTimeInWords(new Date());
};

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
speedo-0.0.7 vendor/assets/javascripts/time_ago_in_words.js
speedo-0.0.6 vendor/assets/javascripts/time_ago_in_words.js