A simple clean way to format intervals with Javascript
Setup
npm install simple-format-timeago --save
Usage
; // handle now; // => 'Just now'// handle past; // => '1 minute ago'// handle plurals; // => '2 minutes ago'// handle future; // => '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 %>' ; ; // => '1 minuto fa'
API
string
where:
date
a date (in the past or the future) or an interval expressed in millisecondsoptions
labels
now
(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'
templates
past
(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