timeago.jsはタイムスタンプを自動的により読みやすい時間書式に変換します.
4464 ワード
timeago.js
timeago.js is a simple library(less than Localization supported. Time Real-time render supported. Nodejs and browserjs supported. ウェルテクスト. Official website.中国語版の説明はここにあります.React version here:timeago-react.Python version here:timeago.
Such as
1.Install timeago.js
ES 6 style is supported、then get global oject:
1.Localization
Default locale is
HTMLコード:
The API
The DOM object shuld have the atribute
5.Register local langage
You can
Locale contributions are welcomped、thank you for submitting a GigtHub purl request for corections or additional langages.^u^~
3.Conttributions The website is based on rmm 5 t/jquery-timeago which is a nice and feature d project but depends on jQuery. locale translations:The library needs more locale trans.You can: Open an issue to write the local translations、or submit a pull request.How to?see en's translation. Please test the locale by exec 4.LICENSE
MIT
timeago.js is a simple library(less than
2 kb
)that is used to format dateme with *** time ago
statement.eg:'3 houss ago'.ago
and time in
supported.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
1.Usage1.Install timeago.js
npm install timeago.js
2.import timeago.jsES 6 style is supported、then get global oject:
timeago
.import timeago from 'timeago.js';
// or
var timeago = require("timeago.js");
or link with script
in html files:
3.Use class timeago
var timeagoInstance = new timeago();
timeagoInstance.format('2016-06-12');
2.Detailed Usage1.Localization
Default locale is
en
、and the library supports en
and zh_CN
.var timeagoInstance = new timeago();
timeagoInstance.format('2016-06-12', 'zh_CN');
You can change the locare in the constructor use the setLocale
method;var timeagoInstance = new timeago('zh_CN');
// or
new timeago().setLocale('zh_CN');
2.Set relative datetimeago
is relative to the current date default.You can set it yourself.var timeagoInstance = new timeago(null, '2016-06-10 12:12:12'); // set the relative date here.
timeagoInstance.format('2016-06-12', 'zh_CN');
3.Use timestampnew timeago().format(new Date().getTime() - 11 * 1000 * 60 * 60); // will get '11 hours ago'
4.Automatic rendengHTMLコード:
jsコードvar timeagoInstance = new timeago();
// use render to render it in real time
timeagoInstance.render(document.querySelectorAll('.need_to_be_rendered'), 'zh_CN');
// or cancel real-time render
timeagoInstance.cancel()
The input API render
Shuld be DOM object/array、pure javascript DOM node、and jQuery DOM oject supported.The API
cancel
will clear all the render timers and release all reource of the instance.The DOM object shuld have the atribute
datetime
or data-timeago
with date formasted string.5.Register local langage
You can
register
your own langgage、this a class static method.Like below、e.g.// the local dict example is below.
var test_local_dict = function(number, index) {
// number: the timeago / timein number;
// index: the index of array below;
return [
['just now', 'a while'],
['%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];
};
// register your locale with timeago
timeago.register('test_local', test_local_dict);
// use the locale with timeago instance
var timeagoInstance = new timeago();
timeagoInstance.format('2016-06-12', 'test_local');
You can see locaes dir for more locaes.Locale contributions are welcomped、thank you for submitting a GigtHub purl request for corections or additional langages.^u^~
3.Conttributions
npm test
or node tests/locales_test.js
.How to write testcase,see en's test cases.MIT