Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moment.js works with valid date on Chrome but not IE or 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 and Chrome just return "Invalid Date"

any ideas what im missing?

like image 814
D-W Avatar asked Nov 16 '15 08:11

D-W


People also ask

How do you check if a date is valid or not using moment?

isValid() is the method available on moment which tells if the date is valid or not. MomentJS also provides many parsing flags which can be used to check for date validation.

Is MomentJS still used?

MomentJS is a widely used time and date formatting and calculation library.

Why you dont need MomentJS?

You do not need Moment. js if you support newer browsers and if you show dates or datetimes only in your user's timezone. Things are not as easy as they seem, especially if you plan on manually parsing the output from the native APIs.

Is JavaScript moment immutable?

Mutability 1.0.The moment object in Moment. js is mutable. This means that operations like add, subtract, or set change the original moment object.


1 Answers

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

like image 60
Gabriel Sadaka Avatar answered Nov 03 '22 00:11

Gabriel Sadaka