timeago.js
a tiny / simple library
What ?
timeago.js is a tiny(2kb) library used to format date with `*** time ago`. eg: '3 hours ago'. No dependence. and support automatically updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago"). Download, view the examples, and enjoy.
You opened this page when you opened the page. (This will update automatically. Wait for it.)
Hustcc was born Aug 01, 1989.
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.
- You get to use standard HTML5 tags.
- Very tiny(only 2kb), and has no dependence, e.g. jQuery.
How ?
First, load and install timeago.js library:
npm install timeago.js
Load timeago.js library file with script tag:
<script src="dest/timeago.js" type="text/javascript"></script>
Or you can use ES6 style:
import timeago from 'timeago.js';
// or
var timeago = require("timeago.js");
Now, let's attach it to your timestamps on DOM ready:
timeago().render(document.querySelectorAll('.need_to_be_rendered'));
// or use jquery selector
timeago().render($('.need_to_be_rendered'));
This will turn all time elements with a class of timeago and an ISO 8601 timestamp in the datetime (other datetime styles are also supported):
<span class="need_to_be_rendered" data-timeago="2016-07-07T09:24:17Z">July 07, 2016</span>
You can also use it programmatically:
timeago().format(new Date()); //=> ""
timeago().format("2016-09-07", 'zh_CN'); //=> ""
timeago().format(1473245023718); //=> ""
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 timeago = timeago();
var locale = [
['há pouco', 'daqui um pouco'],
['há %s segundos', 'em %s segundos'],
['há um minuto', 'em um minuto'],
['há %s minutos', 'em %s minutos'],
['há uma hora', 'em uma hora'],
['há %s horas', 'em %s horas'],
['há um dia', 'em um dia'],
['há %s dias', 'em %s dias'],
['há uma semana', 'em uma semana'],
['há %s semanas', 'em %s semanas'],
['há um mês', 'em um mês'],
['há %s meses', 'em %s meses'],
['há um ano', 'em um ano'],
['há %s anos', 'em %s anos']
];
timeago.register('pt_BR', locale);
// then you can use it
timeago().format(1473245023718, 'pt_BR');
Where ?
Package is managed with npm. so npm install timeago.js will be the lastest version.
The code is hosted on GitHub: http://github.com/hustcc/timeago.js. Go on, live on the edge.
Who?
timeago.js was built by Hustcc while standing on the shoulders of giants. This website is base on jQuery-timeago.
What else?
Other timeago, welcome to pull a request.
Do you use timeago.js ?
Great! Please let me know, and can add into the README.