简体   繁体   English

当日期为MM / DD / YYYY HH:mm:ss时,moment.js从现在的日期返回意外

[英]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"; 我有以下格式的日期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. 我正在尝试从时间戳获取相对时间(4小时前,3分钟前,3天前,等等),与从现在开始使用的当前日期时间进行比较。

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

but this is returning "2010 years from now" . 但这正在返回"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? 为了使fromNow方法能够按预期工作,是否需要以其他方式设置日期格式?

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 . HH表示24小时制,但您正在使用12小时制,为此您需要使用hh Also, A will match AM/PM . 另外, A将匹配AM/PM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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