Moment.js在Firefox中提供无效日期 [英] Moment.js gives Invalid date in Firefox

查看:391
本文介绍了Moment.js在Firefox中提供无效日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在moment.js中转换日期时间。但它在Chrome和Firefox中给出了不同的结果。

I have a requirement to convert date time in moment.js. But it gives me different result in Chrome and Firefox.

在谷歌浏览器中它提供了正确的结果,但在Mozilla中,Firefox提供了无效日期。

In Google Chrome it gives correct result but in Mozilla firefox gives "Invalid date".

Google Chrome

Google chrome

moment('2016-Jan-02 02:00 AM').format()
Output: "2016-01-02T02:00:00+05:30"

Mozilla firefox

Mozilla firefox

moment('2016-Jan-02 02:00 AM').format()
"Invalid date"

非常感谢您的帮助。

推荐答案

建议避免使用自定义格式的时刻解析。正如文档说明

It's recommended to avoid using moment parsing with custom format. As the documentation states:

警告:浏览器对解析字符串的支持不一致。因为没有关于应该支持哪些格式的规范,所以在某些浏览器中有效的方法在其他浏览器中不起作用。

Warning: Browser support for parsing strings is inconsistent. Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers.

为了解析除ISO 8601字符串以外的任何其他内容的一致结果,你应该使用字符串+格式。

For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.

在你的情况下,一致解析的代码将是:

In your case, the code for consistent parsing will be:

moment('2016-Jan-02 02:00 AM', 'YYYY-MMM-DD HH:mm A')

这篇关于Moment.js在Firefox中提供无效日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆