This repository has been archived on 2024-08-27. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
redstoner.com/app/assets/javascripts/ago.js
2014-05-02 05:12:16 +02:00

31 lines
778 B
JavaScript

$.fn.ago = function(callback) {
units = [
['m', 60],
['h', 3600],
['d', 86400],
['w', 604800],
['y', 31536000]
];
this.each(function() {
ago_date = callback ? callback(this) : new Date($(this).text());
ago_time = Math.floor((new Date().getTime() - ago_date.getTime())/1000);
ago_unit = null;
units.forEach(function(time, i) {
if (Math.abs(ago_time) >= time[1]) {
ago_unit = i;
} else {
// we found the greatest unit
return;
}
});
if (ago_unit !== null) {
unit = units[ago_unit];
ago_str = Math.abs(Math.floor(ago_time/unit[1])).toString() + unit[0] + (ago_time < 0 ? " ahead" : " ago");
} else {
ago_str = "just now";
}
$(this).text(ago_str);
});
};