timeago.js
timeago.js is a nano library(less than
2 kb
) used to format datetime with*** time ago
statement. eg: '3 hours ago'.
- i18n supported.
- Time
ago
and timein
supported. - Real-time render supported.
- Node and browser supported.
- Well tested.
Official website. React version here: timeago-react. Python version here: timeago.
Such as
just now12 seconds ago2 hours ago3 days ago3 weeks ago2 years ago in 12 secondsin 3 minutesin 24 daysin 6 months
Usage
- install
npm install timeago.js
- import
;
or import with script
tag in html file and access global variable timeago
.
- example
// format the time with localeformat'2016-06-12', 'en_US';
API
There only 4 API below.
- format
format(date[, locale = 'en_US', opts])
, format a Date instance / timestamp / date string to string.
; // format timestampformat1544666010224; // format date instanceformatnew Date1544666010224; // format date stringformat'2018-12-12'; // format with localeformat1544666010224, 'zh_CN'; // format with locale and relative dateformat1544666010224, 'zh_CN', ; // e.g.formatDate.now - 11 * 1000 * 60 * 60; // returns '11 hours ago'
The default locale is en_US
, and the library contains en_US
and zh_CN
build-in.
- render & cancel
render(dom[, locale = 'en_US', opts])
cancel([dom])
Make a dom with
datetime
attribute automatic rendering and cancel.
HTML code:
Javascript code:
; ; // use render method to render nodes in real timerendernodes, 'zh_CN'; // render with opts// render(nodes, 'en_US', { minInterval: 3 }); // cancel all real-time render taskcancel; // or cancel for the specific onecancelnodes
The 3rd parameter opts
contains:
;
The DOM object should have the attribute
datetime
with date formatted string.
- register
register(locale, localeFunc)
, register a new locale, build-in locale contains:en_US
,zh_CN
, all locales here.
You can register your own language with register
API.
;// register your locale with timeagoregister'my-locale', localeFunc; // use itformat'2016-06-12', 'my-locale';
Contributions
- The website is based on rmm5t/jquery-timeago which is a nice and featured project but it depends on jQuery.
- locale translations: The library needs more locale translations. You can:
- Open an issue to write the locale translations, or submit a pull request. How to ? see locales translation.
- Please test the locale by exec
npm test
. How to write test cases, see locales test cases.
LICENSE
MIT@hustcc