npm.io
1.1.0 • Published 1 year ago

@yaireo/relative-time

Licence
MIT
Version
1.1.0
Deps
0
Size
17 kB
Vulns
0
Weekly
0
Stars
42

Transform timestamp or date to local relative-time


Super lightweight (< 500 bytes)     Uses native browser API


Install:

NPM / CDN / Download from this repo

npm i @yaireo/relative-time -S

Usage:

Import:

import RelativeTime from '@yaireo/relative-time'

And use:

```js
const relativeTime = new RelativeTime(); // defaults to OS locale
const relativeTimeSpanish = new RelativeTime({ locale: 'es' }); // set Spanish locale

console.log(   relativeTime.from(new Date('2020'))  )  // "6 years ago"
console.log(   relativeTimeSpanish.from(new Date('2020'))  )  // "hace 6 años"

Or without instanciation:

console.log(RelativeTime.from(new Date('2032'))) // "in 6 years"

Live demo

Keywords