Skip to content

jcubic/timeago.js

 
 

Repository files navigation

timeago.js

timeago.js is a simple library (less then 2.1 kb) to used to format datetime with *** time ago statement. eg: '3 hours ago'.

  • localization supported.
  • time ago, time in supported.
  • real time render supported.
  • npm and browser supported.
  • well tesed.

Official website. 中文版说明文档点这里。 React version here: timeago-react. Python version here: timeago.

Build Status npm npm npm

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

Usage

1. Install timeago.js

npm install timeago.js

2. import timeago.js

UMD import is supported, then get global object: timeago.

import timeago from 'timeago.js';

// or

var timeago = require("timeago.js");

or link with script in html files:

<script src="dist/timeago.min.js"></script>

3. use class timeago

var timeago = timeago();
timeago.format('2016-06-12')

Detail Usage

1. set relative date

timeago is relate to current date default. you can set it by yourself.

var timeago = timeago('2016-06-10 12:12:12'); // set the relative date here.
timeago.format('2016-06-12', 'zh_CN');

2. use timestamp

timeago().format(new Date().getTime() - 11 * 1000 * 60 * 60); // will get '11 hours ago'

3. automation render

HTML code:

<div class="need_to_be_rendered" data-timeago="2016-06-30 09:20:00"></div>

js code

// use render to render it realtime
timeago().render(document.querySelectorAll('.need_to_be_render'), 'zh_CN');
// or cancel realtime render
timeago().cancel()

The input API render should be DOM object / array, support pure javascript node and jQuery dom object.

The API cancel will clear all the render timer, release all the resource.

the dom object should has attribute data-timeago with date formated string.

4. localization

Default locale is en, and the library supports en and zh_CN.

var timeago = timeago();
timeago.format('2016-06-12', 'zh_CN');

You can change the locale in the constructor, or use the setLocale method;

var timeago = timeago(null, 'zh_CN');
// or
timeago.setLocale('zh_CN');

5. register local language

You can register you own language. All keys are needed. 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];
};

var timeago = timeago();
timeago.register('test_local', test_local_dict);

timeago.format('2016-06-12', 'test_local');

You can see locales dir for more locales. Please submit a GitHub pull request for corrections or additional languages, and add the locale key into tests/locales_test.js.

Contribution

The website is base on rmm5t/jquery-timeago which is a nice and featured project but depends on jQuery.

LICENSE

MIT

About

🕗 ⌛ timeago.js is a tiny(~2kb) library used to format date with `*** time ago`. eg: '3 hours ago'. No dependences.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 72.8%
  • HTML 27.2%