Moment.js在Chrome上具有有效日期,但在IE或Firefox上无效 [英] Moment.js works with valid date on Chrome but not IE or Firefox

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

问题描述

因此,这在Chrome浏览器中可以正常运行,但在IE(11)和Firefox上无法运行

So this works fine in Chrome but not IE(11) and Firefox

 var startDate = moment("12-Nov-2015").format("D-MMM-YYYY");
        var startTime = "10:00 AM";

        var startDateTime = moment(startDate + ' ' + startTime);
alert(moment(startDateTime).format("D-MMM-YYYY h:mm A"));

IE和Chrome只会返回无效日期"

IE and Chrome just return "Invalid Date"

有什么想法我想念吗?

推荐答案

这是因为"12-Nov-2015"不是有效的ISO 8601格式,因此MomentJS退回到了浏览器解析器,根据浏览器.之所以会出现此问题,是因为Google Chrome接受该格式,但不接受IE或Firefox,而不是Moment的问题.

This would be because "12-Nov-2015" is not a valid ISO 8601 format therefore MomentJS falls back to the browser parser which is quite different according to the browser. So this issue would be caused because Google Chrome accepts that format but not IE or Firefox, not an issue with Moment.

有关更多详细信息,请参见此链接: http://momentjs.com/docs/# /parsing/string/

Please see this link for more details: http://momentjs.com/docs/#/parsing/string/

如其文档所述,如果使用非ISO 8601格式,则在解析时指定字符串的格式,请使用 http://momentjs.com/docs/#/parsing/string-format/

As their documentation states, if using a non ISO 8601 format specify the format of the string when parsing, using http://momentjs.com/docs/#/parsing/string-format/

所以

var startDate = moment("12-Nov-2015").format("D-MMM-YYYY");

应该是

var startDate = moment("12-Nov-2015", "D-MMM-YYYY").format("D-MMM-YYYY");

请参阅此处以获取有关日期解析不一致的信息: http://dygraphs.com/date-formats. html

Please see here for information in date parsing inconsistencies: http://dygraphs.com/date-formats.html

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

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