Sha256: bc9c39de43ad0fc6de4891c64b4495ed6c6aa755fbf2a7bf25afa013b530e5ed

Contents?: true

Size: 1.83 KB

Versions: 306

Compression:

Stored size: 1.83 KB

Contents

// from http://blog.peelmeagrape.net/2008/7/26/time-ago-in-words-javascript-part-1
function distanceOfTimeInWords(fromTime, toTime, includeSeconds) {
  var fromSeconds = fromTime.getTime();
  var toSeconds = toTime.getTime();
  var distanceInSeconds = Math.round(Math.abs(fromSeconds - toSeconds) / 1000)
  var distanceInMinutes = Math.round(distanceInSeconds / 60)
  if (distanceInMinutes <= 1) {
    if (!includeSeconds)
      return (distanceInMinutes == 0) ? 'less than a minute' : '1 minute'
    if (distanceInSeconds < 5)
      return 'less than 5 seconds'
    if (distanceInSeconds < 10)
      return 'less than 10 seconds'
    if (distanceInSeconds < 20)
      return 'less than 20 seconds'
    if (distanceInSeconds < 40)
      return 'half a minute'
    if (distanceInSeconds < 60)
      return 'less than a minute'
    return '1 minute'
  }
  if (distanceInMinutes < 45)
    return distanceInMinutes + ' minutes'
  if (distanceInMinutes < 90)
    return "about 1 hour" 
  if (distanceInMinutes < 1440)
    return "about " + (Math.round(distanceInMinutes / 60)) + ' hours'
  if (distanceInMinutes < 2880)
    return "1 day" 
  if (distanceInMinutes < 43200)
    return (Math.round(distanceInMinutes / 1440)) + ' days'
  if (distanceInMinutes < 86400)
    return "about 1 month" 
  if (distanceInMinutes < 525600)
    return (Math.round(distanceInMinutes / 43200)) + ' months'
  if (distanceInMinutes < 1051200)
    return "about 1 year" 
  return "over " + (Math.round(distanceInMinutes / 525600)) + ' years'
}

if ( typeof jQuery != "undefined" )
	jQuery.fn.readableDate = function(){
		return this.each(function(){
			var span = jQuery(this);
			var date = distanceOfTimeInWords(new Date(span.text()), new Date(), true);
			if (date)
				jQuery(this).text(date + ' ago');
		});
	};

jQuery(document).ready(function() {
	jQuery(".readable-time").readableDate();
});

Version data entries

306 entries across 248 versions & 14 rubygems

Version Path
muck-engine-3.5.0 public/javascripts/muck_time/en.js
muck-engine-3.5.0 test/public/javascripts/muck_time/en.js
muck-activities-3.2.2 test/public/javascripts/muck_time/en.js
muck-engine-3.4.0 test/public/javascripts/muck_time/en.js
muck-engine-3.4.0 public/javascripts/muck_time/en.js
muck-engine-3.3.18 test/public/javascripts/muck_time/en.js
muck-engine-3.3.18 public/javascripts/muck_time/en.js
muck-activities-3.2.1 test/public/javascripts/muck_time/en.js
muck-engine-3.3.17 public/javascripts/muck_time/en.js
muck-engine-3.3.17 test/public/javascripts/muck_time/en.js
muck-engine-3.3.16 test/public/javascripts/muck_time/en.js
muck-engine-3.3.16 public/javascripts/muck_time/en.js
muck-engine-3.3.15 test/public/javascripts/muck_time/en.js
muck-engine-3.3.15 public/javascripts/muck_time/en.js
muck-engine-3.3.14 public/javascripts/muck_time/en.js
muck-engine-3.3.14 test/public/javascripts/muck_time/en.js
muck-services-3.3.3 test/public/javascripts/muck_time/en.js
muck-users-3.2.12 test/public/javascripts/muck_time/en.js
muck-users-3.2.11 test/public/javascripts/muck_time/en.js
muck-engine-3.3.13 test/public/javascripts/muck_time/en.js