Moment.js works with valid date on Chrome but not

2019-04-30 05:51发布

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 and Chrome just return "Invalid Date"

any ideas what im missing?

1条回答
神经病院院长
2楼-- · 2019-04-30 06:21

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.

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

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/

So

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

Should be

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

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

查看更多
登录 后发表回答