Lightweight Human Readable Date & Time Library – timeago.js

Category: Date & Time , Javascript , Recommended | December 14, 2018
Author: hustcc
Views Total: 2,342
Official Page: Go to website
Last Update: December 14, 2018
License: MIT

Preview:

Lightweight Human Readable Date & Time Library – timeago.js

Description:

timeago.js is a lightweight and pure JavaScript version of the familiar jQuery timeago plugin that allows to parse and format dates & times using natural language like ‘5 minutes ago’.

Basic usage:

Install and import the timeago.js in your web project.

npm install timeago.js
import timeago from 'timeago.js';
<script src="dist/timeago.min.js"></script>

Create a new timeago object and format a specified date time as follow:

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

Format and render the date & time inside a specific container.

<div class="demo" data-timeago="2016-09-12 00:20:00"></div>
timeago().render(document.querySelectorAll('.demo')');

Customize the output strings.

var myDict = function(number, index) {
    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('myStrings', myDict);

timeago.format('2016-09-12', 'myStrings');

Changelog:

12/14/2018

  • v4.0.0-beta.2

05/27/2018

  • v4.0.0

06/21/2017

  • v3.0.2

You Might Be Interested In:


Leave a Reply