0.0.1 • Published 10 years ago
simple-format-timeago v0.0.1
A simple clean way to format intervals with Javascript
Setup
npm install simple-format-timeago --saveUsage
import formatTimeago from 'simple-format-timeago';
// handle now
formatTimeago(new Date()); // => 'Just now'
// handle past
formatTimeago(60 * 1000); // => '1 minute ago'
// handle plurals
formatTimeago(2 * 60 * 1000); // => '2 minutes ago'
// handle future
formatTimeago(-2 * 60 * 1000); // => '2 minutes from now'Changing i18n
var it = { // italian
labels: {
now: 'Adesso',
seconds: 'secondi',
second: 'secondo',
minutes: 'minuti',
minute: 'minuto',
hours: 'ore',
hour: 'ora',
days: 'giorni',
day: 'giorno',
weeks: 'settimane',
week: 'settimana',
months: 'mesi',
month: 'mese',
years: 'anni',
year: 'anno'
},
templates: {
past: '<%= count %> <%= unit %> fa',
future: 'tra <%= count %> <%= unit %>'
}
};
formatTimeago(60 * SECOND, it); // => '1 minuto fa'API
formatTimeago(date: Date | number, options: Object) => stringwhere:
datea date (in the past or the future) or an interval expressed in millisecondsoptionslabelsnow(default'Just now')seconds(default'seconds')second(default'second')minutes(default'minutes')minute(default'minute')hours(default'hours')hour(default'hour')days(default'days')day(default'day')weeks(default'weeks')week(default'week')months(default'months')month(default'month')years(default'years')year(default `'year'
templatespast(default'<%= count %> <%= unit %> ago')future(default'<%= count %> <%= unit %> from now')
The following locals will be passed in to templates:
{
count: number; // the current count expressed in unit
unit: string; // the current unit
labels: {[key: string]: string;} // all labels
}0.0.1
10 years ago