timeAgo v2
It is native Javascript function for automatically updating fuzzy timestamps (e.g. "8 minutes ago").
by sinky
HTML
<time datetime="1373279025" title="2012-07-08T08:33:51Z" class="js-relative-date">2013-07-08T08:33:51Z</time>
<abbr class="js-relative-date" title="2011-12-17T09:24:17Z">2011-12-17T09:24:17Z1</abbr>
<abbr class="js-relative-date" title="December 17, 2012">December 17, 2012</abbr>
<abbr class="js-relative-date" title="1360000001">Timestamp</abbr>
<time class="js-relative-date" datetime="2013-01-17T09:24:17Z">2013-01-17T09:24:17Z</time>
<span class="js-relative-date" title="1372407476">1372407476</span>
CSS
body, abbr, time, span {
font-family: sans-serif;
display: block;
padding: 5px;
}
JavaScript
/**
* Returns a description of this date in relative terms.
* Examples, where new Date().toString() == "Mon Nov 23 2009 17:36:51 GMT-0500 (EST)":
*
* new Date().toRelativeTime()
* --> 'Just now'
*
* new Date("Nov 21, 2009").toRelativeTime()
* --> '2 days ago'
*
* new Date("Nov 25, 2009").toRelativeTime()
* --> '2 days from now'
*
* // One second ago
* new Date("Nov 23 2009 17:36:50 GMT-0500 (EST)").toRelativeTime()
* --> '1 second ago'
*
* toRelativeTime() takes an optional argument - a configuration object.
* It can have the following properties:
* - now - Date object that defines "now" for the purpose of conversion.
* By default, current date & time is used (i.e. new Date())
* - nowThreshold - Threshold in milliseconds which is considered "Just now"
* for times in the past or "Right now" for now or the immediate future
* - smartDays - If enabled, dates within a week of now will use Today/Yesterday/Tomorrow
* or weekdays along with time, e.g. "Thursday at 15:10:34"
* rather than "4 days ago" or "Tomorrow at 20:12:01"
* instead of "1 day from now"
*
* If a single number is given as argument, it is interpreted as nowThreshold:
*
* // One second ago, now setting a now_threshold to 5 seconds
* new Date("Nov 23 2009 17:36:50 GMT-0500 (EST)").toRelativeTime(5000)
* --> 'Just now'
*
* // One second in the future, now setting a now_threshold to 5 seconds
* new Date("Nov 23 2009 17:36:52 GMT-0500 (EST)").toRelativeTime(5000)
* --> 'Right now'
*
*/
Date.prototype.toRelativeTime = (function () {
var _ = function (options) {
var opts = processOptions(options);
var now = opts.now || new Date();
var delta = now - this;
var future = (delta <= 0);
delta = Math.abs(delta);
// special cases controlled by options
if (delta <= opts.nowThreshold) {
return future ? 'Right now' : 'Just now';
}
...