Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 767
cusejuice Avatar asked Nov 22 '25 08:11

cusejuice


1 Answers

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
like image 171
squiroid Avatar answered Nov 24 '25 21:11

squiroid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!