I am converting a simple dateString to Date object. The following code works perfectly on all browsers except Firefox.
var dateString = "02-24-2014 09:22:21 AM";
var dateObject = new Date(dateString);
console.log(dateObject.toDateString());
The Firebug console in Firefox says Invalid Date. What am I doing wrong here?
I also tried replacing - with \, but it didnt help.
Is it possible to do this without using any libraries?
Looks like Firefox does not like the - in dateString.
Replace all occurrences of - with / using a regular expression and then convert the string to Date object.
var str = '02-24-2014 09:22:21 AM';
str = str.replace(/-/g,'/'); // replaces all occurances of "-" with "/"
var dateObject = new Date(str);
alert(dateObject.toDateString());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With