Timeago

Overview

This little JavaScript library turns absolute dates (e.g. 2013-04-27 22:58:43) into a relative time (e.g. 3 minutes ago)

There are various libraries that offer this functionality. Where this one differs is that it uses a single ‘global’ setTimeout call that happens once per second, rather than a separate setInterval for every element.

This is especially useful when there are a large number of relative dates in a page - reducing the number of timeouts is always a good idea.

Usage

Create an HTML element with a datetime data attribute:

1
<p class="js-relative" data-datetime="2013-04-27 22:58:43">2013-04-27 22:58:43</p>

To start timeago, call:

1
timeAgo.init();

To add a relative date, call:

1
2
var $el = $(".js-relative");
timeAgo.add($el);

(where $el is a jQuery object containing the element)