Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

timeago.js

Package Overview
Dependencies
Maintainers
2
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

timeago.js

timeago.js is a simple library (only 1kb) to used to format datetime with `*** time ago` statement. eg: '3 hours ago'. localization supported.

  • 4.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
318K
decreased by-4.62%
Maintainers
2
Weekly downloads
 
Created

What is timeago.js?

timeago.js is a lightweight library used to format dates in a 'time ago' style, such as '3 minutes ago' or '2 days ago'. It is useful for displaying relative timestamps in a human-readable format.

What are timeago.js's main functionalities?

Basic Usage

This feature allows you to format the current date and time into a 'time ago' format. The code initializes a timeago instance and formats the current date.

const timeago = require('timeago.js');
const timeagoInstance = timeago();
console.log(timeagoInstance.format(new Date()));

Custom Locale

This feature allows you to register a custom locale for formatting dates. The code demonstrates how to create a custom locale and use it to format the current date.

const timeago = require('timeago.js');
const timeagoInstance = timeago();
timeago.register('my-locale', (number, index, totalSec) => [
  ['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]);
console.log(timeagoInstance.format(new Date(), 'my-locale'));

Live Updates

This feature allows you to automatically update the 'time ago' text as time passes. The code creates a DOM element, sets its datetime attribute, and uses timeago.js to render and update the text.

const timeago = require('timeago.js');
const timeagoInstance = timeago();
const element = document.createElement('div');
element.setAttribute('datetime', new Date().toISOString());
document.body.appendChild(element);
timeagoInstance.render(element);

Other packages similar to timeago.js

Keywords

FAQs

Package last updated on 21 Dec 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc