Stavros_S
Stavros_S

Reputation: 2235

moment.js returning an unexpected fromNow date when date is MM/DD/YYYY HH:mm:ss

I have a date in the following format var timestamp = "6/9/2016 1:47:31 PM";. I'm trying to get the relative time (4 hours ago, 3 minutes ago, 3 days ago, etc...) from the timestamp compared to the current datetime using from now.

var LastReading = moment(timestamp).fromNow();

but this is returning "2010 years from now". I tried using the format

var LastReading = moment(timestamp, "MM/DD/YYYY HH:mm:ss").fromNow();

but I get the same result. Any ideas? Do I need to format the date in a different way in order to get the fromNow method to work as expected?

Upvotes: 0

Views: 1279

Answers (1)

robertklep
robertklep

Reputation: 203329

To match your timestamp, the format should look like this:

MM/DD/YYYY hh:mm:ss A

HH means 24 hour time, but you're using 12 hour time, for which you need to use hh. Also, A will match AM/PM.

Upvotes: 2

Related Questions