1.0.0 • Published 4 years ago

ago-i18n v1.0.0

Weekly downloads
5
License
MIT
Repository
github
Last release
4 years ago

ago-i18n

Fully unit tested module to convert Date objects into human readable relative timestamps, such as '4 minutes ago', 'yesterday', 'tomorrow', or 'in 3 months'.

Adapted from the excellent work of Sebastian Sandqvist available as s-ago

This version is a javascript adaptation (original in typescript) and adds i18n capabilities and a spanish version. It is easily translatable, refer to i18n documentation

Usage

var ago = require("ago-i18n");

// Set locale "en" or "es" (en by default)
ago.locale = "en";

var now = new Date();
var yesterday = new Date(now.getTime() - 24 * 60 * 60 * 1000);
var hoursAgo = new Date(now.getTime() - 6 * 60 * 60 * 1000);
var yesterday = new Date(now.getTime() - 24 * 60 * 60 * 1000);
var tomorrow = new Date(now.getTime() + 6 * 60 * 60 * 1000);
var inSixHours = new Date(now.getTime() + 6 * 60 * 60 * 1000);

// present
ago(now); // 'just now'

// past
ago(yesterday); // 'yesterday'
ago(hoursAgo); // '6 hours ago'

// future
ago(inSixHours); // 'in 6 hours'
ago(tomorrow); // 'tomorrow'

Output is as follows:

TimeOutputFuture output
Less than 1 minutejust nowjust now
1-2 minutesa minute agoin a minute
2-46 minutes# minutes agoin # minutes
46 minutes - 2 hoursan hour agoin an hour
2-20 hours# hours agoin # hours
20-48 hoursyesterdaytomorrow
2-6 dayslast weekin a week
7-28 days# weeks agoin # weeks
28 days - 2 monthslast monthin a month
2-11 months# months agoin # months
11-23 monthslast yearin a year
2+ years# years agoin # years