Skip to content
🕗 ⌛️ timeago.js is a tiny(~2.0kb) library used to format date with `*** time ago` statement. eg: '3 hours ago'. No dependency & localization & tiny.
Branch: master
Clone or download
sky93 and hustcc Change regular numbers to standard persian numbers in Farsi locale (#177
)

* Change regular numbers to standard persian numbers

In standard persian numbers we have ۰, ۱, ۲, ۳, ..., ۹ instead of 0. 1, 2, 3, ..., 9

* Change "الآن" to "حالا"

"حالا" is more popular than "الآن"

* Convert regular numbers to standard persian number

Convert non-standard numbers like 1, 2, 3, ... to ۱, ۲, ۳, ... using a find and replace function.

* Fix feature dates

Future dates translated totally wrong. They were not actually future tense! So I fixed all of them. I also swapped "لحظاتی پیش" with "همین حالا"

* Create testcase for fa locale

Create testcase for Farsi (Persian) language.
Latest commit 1c58c61 Feb 21, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
__tests__ Change regular numbers to standard persian numbers in Farsi locale (#177 Feb 21, 2019
gh-pages Use jQuery 3.3.1 hosted on jQuery CDN for the demo Jan 6, 2019
src Change regular numbers to standard persian numbers in Farsi locale (#177 Feb 21, 2019
.babelrc refactor Dec 12, 2018
.gitignore
.npmignore
.travis.yml
LICENSE v1.0.0 Jun 23, 2016
README.md
README_zh.md fix typo for README_zh.md and add 个 for describe month in src/lang/zh… Dec 14, 2018
bower.json update types Dec 13, 2018
package.json resolve 'NODE_ENV' is not recognised when running test in windows Dec 15, 2018
rollup.config.all.js refactor Dec 12, 2018
rollup.config.js update README Dec 13, 2018
timeago.d.ts update types Dec 13, 2018

README.md

timeago.js

timeago.js is a simple library (less than 1 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.

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

Build Status Coverage Status gzip 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

  • Install
npm install timeago.js
  • Import
// ES6
import { format, render, cancel, register } from 'timeago.js';

// commonjs
const { format, render, cancel, register } = require('timeago.js');

or import with script in html file and access global variable timeago.

<script src="dist/timeago.min.js"></script>
  • Usage
// format the time with locale
format('2016-06-12', 'en_US');

API

There only 4 API:

  • format(date[, locale = 'en_US', relativeDate = new Date()]): format a Date instance / timestamp / date string to string.
  • render(dom[, locale = 'en_US', relativeDate = new Date()]): make a dom automatic rendering.
  • cancel([dom]): cancel automatic rendering.
  • register(locale, localeFunc): register a new locale, build-in locale contains: en_US, zh_CN.

Case

  • format

Just format date into a string.

import { format, render, cancel, register } from 'timeago.js';

// format timestamp
format(1544666010224);
// format date instance
format(new Date(1544666010224));
// format date string
format('2018-12-12');

// format with locale
format(1544666010224, 'zh_CN');

// format with locale and relative date
format(1544666010224, 'zh_CN', '2018-11-11');

// e.g.
format(Date.now() - 11 * 1000 * 60 * 60); // returns '11 hours ago'
  • render & cancel

You can render a dom node with automatic rendering.

HTML code:

<div class="needs_to_be_rendered" datetime="2016-06-30 09:20:00"></div>

Javascript code:

var nodes = document.querySelectorAll('.needs_to_be_rendered');

// use render method to render nodes in real time
timeago.render(nodes, 'zh_CN');

// cancel all real-time render task
timeago.cancel();

// or cancel for the specific one
timeago.cancel(nodes[0])

The input for render method should be DOM object / array, pure javascript DOM node or jQuery DOM object supported.

The cancel method clears all the render timers and release all resources of the instance. Optionally it accepts a single node to cancel timer just for it.

The DOM object should have the attribute datetime with date formatted string.

  • register

Default locale is en_US, and the library supports en_US and zh_CN. You can register your own language with register.

// the local dict example is below.
const localeFunc = (number, index, total_sec) => {
  // number: the timeago / timein number;
  // index: the index of array below;
  // total_sec: total seconds between date to be formatted and today's date;
  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];
};
// register your locale with timeago
register('my-locale', localeFunc);

// use it
format('2016-06-12', 'my-locale');

Check out more locales.

Contributions

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

  2. 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 testcase, see locales test cases.

LICENSE

MIT@https://github.com/hustcc

You can’t perform that action at this time.