Creating new date object with format (mm/dd/yyyy) fails in IE11

时间:2016-08-31 18:34:37

标签: javascript internet-explorer

This fails in IE 11 but works fine in Chrome. //where startDate is "10/17/2016"

var testDate1 = new Date(startDate);

This gives an invalid date in IE with the debugger, but works fine in Chrome.

According to MSDN, this should work https://msdn.microsoft.com/en-us/library/ff743760(v=vs.94).aspx

Another example, just playing with the date format enter image description here

Am I missing something?

1 个答案:

答案 0 :(得分:0)

For certain result you may split datestring and use new Date function as below. On Date constructor Months are array 0 to 11. So we have to use -1. If your datestring is not a valid date then below function returns null.

function MakeDate(DString) /*10/17/2016*/
{ DString=DString.split('/');
return new Date(DString[2], parseInt(DString[0])-1, DString[1]);
}