Download
User Rating: 3.6/5 ( 4 votes)
timeago.js is a simple library (less than 2 kb) that is used to format datetime with *** time ago statement. eg: '3 hours ago'.
-
Localization supported.
-
Time ago and time in supported.
-
Real-time render supported.
-
Nodejs and browserjs supported.
-
Well tested.
Why ?
timeago.js was built to format date with `*** time ago` statement.
-
Avoid timestamps dated "1 minute ago" even though the page was opened 10 minutes ago;timeago refreshes automatically.
-
You can take full advantage of page caching in your web applications, because the timestamps aren't calculated on the server.
-
Very tiny(less than 2kb), and has no dependency, e.g. jQuery.
Such as
just now
12 seconds ago
3 minutes ago
2 hours ago
3 days ago
3 weeks ago
6 months ago
2 years ago
in 12 seconds
in 3 minutes
in 2 hours
in 24 days
in 6 months
in 2 years
Source: timeago.org
1. INCLUDEJS FILES
<script src="dist/timeago.js" type="text/javascript"></script>
2. HTML
<span class="need_to_be_rendered" datetime="2016-07-07T09:24:17Z">July 07, 2016</span>
3. JAVASCRIPT
new timeago().render(document.querySelectorAll('.need_to_be_rendered'));
4. LOCALES REGISTER
Yes, timeago.js has locale/i18n/language support. Here are some configuration examples. Please submit a GitHub pull request for corrections or additional languages.
var locale = function(number, index) {
// number: the timeago / timein number;
// index: the index of array below;
return [
['just now', 'right now'],
['%s seconds ago', 'in %s seconds'],
['1 minute ago', 'in 1 minute'],
['%s minutes ago', 'in %s minutes'],
['1 hour ago', 'in 1 hour'],
['%s hours ago', 'in %s hours'],
['1 day ago', 'in 1 day'],
['%s days ago', 'in %s days'],
['1 week ago', 'in 1 week'],
['%s weeks ago', 'in %s weeks'],
['1 month ago', 'in 1 month'],
['%s months ago', 'in %s months'],
['1 year ago', 'in 1 year'],
['%s years ago', 'in %s years']
][index];
};
timeago.register('pt_BR', locale);
var timeagoInstance = timeago();
// then you can use it
timeagoInstance.format(1473245023718, 'pt_BR');