Skip to content
Advertisement

Using moment to format time in relation to now

I’m trying to display a date like “a few seconds ago”, “10 minutes ago”, “a day ago” with momentjs in the browser.

var date = moment('2017-01-10T13:53:00');
date = moment(date).fromNow();

date returns “in 14 minutes” NOT “14 minutes ago”. How can I fix this?

Advertisement

Answer

Because you would have been comparing it in that way only. Now should be less than the date to show ago.

    var date = moment('2017-01-11T00:01:00');
    date = moment(date).fromNow();
    console.log(date);

//15 minutes ago

Current Date is “Wed Jan 11 2017 00:16:32 GMT+0530 (IST)”

var date = moment('2017-01-11T01:01:00');
date = moment(date).fromNow();
console.log(date);

//in 44 minutes
Advertisement