简体   繁体   中英

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM