Error while converting date string to Date object in Firefox

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());

Try: var dateString = "02/24/2014 09:22:21 AM"

dd-mm-yyyy is not a standard date format in EcmaScript. Some browsers implement it, some don't.

You tried replaceing hyphens with baskslashes, but you need to replace them with slashes.

if a date with hyphens comes from your server or something you can replace them using replace method and regex:

var dateString = "02-24-2014 09:22:21 AM";
dateString = dateString.replace(/-/g, '/');

Tags:

Javascript