Reputation: 3355
Is there a way to use Moment's fromNow()
function but only to a certain point? I want to setup my script so it uses fromNow()
until it exceeds 24 hours, and then I want it to display the full timestamp. What is the best way to achieve this?
moment.locale('en')
moment.tz('America/Los_Angeles')
let time = moment('2017-04-12T17:37:06.886Z').fromNow()
I wasn't able to locate any example in the Moment docs that would allow me to easily achieve this.
Upvotes: 2
Views: 50
Reputation: 364
Better to write your custom fromNow function.
function myFromNow(time) {
let now = moment();
return now.isSame(time, 'day') ? time.fromNow() : time.format();
}
let time1 = moment('2017-04-12T17:37:06.886Z');
let time2 = moment('2018-10-12T17:37:06.886Z');
console.log(myFromNow(time1)); //2017-04-12T19:37:06+02:00
console.log(myFromNow(time2)); //in 5 hours
Upvotes: 4