Recently I blogged about replacing Moment.js with a lightweight alternative Day.js.
In Laravel, when working with dates I’ve been used to using the Carbon method diffForHumans
.
It gives us a date value relative to an optional given date, but it defaults to now. Examples of output from the diffForHumans method looks like:
1 | - 5 minutes ago |
Rather than:
1 | 2019-05-19 18:08:40 |
We can do the same with Moment.js using the fromNow
method. Given that Day.js is supposed to have the same API as Moment.js I assumed that the fromNow
method would “Just work”.
Turns out that it didn’t. But with some small changes to our code we can get the same fromNow
method working with Day.js.
Day.js has a plugin for working with relative times called “relativeTime”.
If you’ve added day JS using yarn / npm you can just import the relativeTime module, extend Day.js and get to using the new fromNow
method!
1 | import dayjs from 'dayjs'; |
To use the fromNow
method within Vue components, I’ve been using filters like this:
1 | <template> |