最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Moment.js returning NaN in Firefox but not chrome - Stack Overflow

programmeradmin2浏览0评论

I have the following line of code:

moment("11-10-2013 09:03 AM").diff(moment(),"minutes");

In Chrome 30.0.1599.101, the following line return a number (It will change every minute so the exact value is not relevant).

In Firefox 25.0, it returns NaN.

I am using the moment.js 2.4.0.

Does anyone understand why this works in Chrome and not FF? I have a feeling it has to do with the way Chrome and Firefox parse date string, but haven't been able to put my finger on the exact reason.

I have the following line of code:

moment("11-10-2013 09:03 AM").diff(moment(),"minutes");

In Chrome 30.0.1599.101, the following line return a number (It will change every minute so the exact value is not relevant).

In Firefox 25.0, it returns NaN.

I am using the moment.js 2.4.0.

Does anyone understand why this works in Chrome and not FF? I have a feeling it has to do with the way Chrome and Firefox parse date string, but haven't been able to put my finger on the exact reason.

Share Improve this question asked Nov 7, 2013 at 15:28 marteljnmarteljn 6,5163 gold badges31 silver badges44 bronze badges 3
  • this is not a fault of moment.js. Date.parse("11-10-2013 09:03 AM") does return NaN in firefox but integer in chrome. – Pranav Gupta Commented Nov 7, 2013 at 15:34
  • 1 Date.parse("11/10/2013 09:03 AM") works though! – Pranav Gupta Commented Nov 7, 2013 at 15:36
  • @PranavGupta You are right! I just arrived at the same conclusion and changed the format of the dates. If you add an answer, I will go ahead an accept it. – marteljn Commented Nov 7, 2013 at 15:40
Add a comment  | 

3 Answers 3

Reset to default 14

Try this

console.log(moment().diff(moment("11-10-2013 09:03 AM", "DD-MM-YYYY hh:mm A"), "minute"));

JSFiddle

Date.parse("11/10/2013 09:03 AM") or Date.parse("11 10 2013 09:03 AM") seem to work in firefox. Chrome seems to be more permissible with delimiters, allowing ., -, and even ; but what's standard it'll only be possible to tell by looking at specifications.

Moment takes milliseconds as a number not a string or at least the raw milliseconds as string has been deprecated. Try this

moment(Number("11-10-2013 09:03 AM"))
发布评论

评论列表(0)

  1. 暂无评论