Date issue in ie8
28 May 2013
I was left stunned when I encountered this wierd IE8 issue. My flow involved taking input from the user via a calender and convert that input into date. I used a datepicker which returned me date string in the format yyyy-mm-dd
.
I took the values selected by user and pass it ot the Date constructor in javascirpt and it worked in FF, Chrome, IE8+. But as soon as i tried the same in IE8 it Bombed
and left me wondering why?
Here's the code:
var dt=new Date("2013-05-28");
What really happend?
MSDN documentation says:
ISO Date Format is not supported in Internet Explorer 8 standards mode and Quirks mode.ISO format is YYYY-MM-DDTHH:mm:ss.sssZ
Solution
Solution 1: To allow IE8 to be able to construct date we need to replace all -
with /
.
var d="2013-05-28"; d=d.replace(/-/g,"/"); var dt=new Date(d);
Solution 2: We can also do something like this.
var d="2013-05-28"; var dt=new Date(); dt.setYear(d.substring(0,4)); dt.setMonth(d.substring(5,7),d.substring(8,10));
More Information
blog comments powered by Disqus