As many of you may know, in JavaScript, you can define a date object by specifying a string that looks like a date.  Recently I created a page using AJAX to get counter information, including the date/time that the page was hit.  I thought that a pretty straight-forward format that JavaScript could understand was “yyyy-mm-dd”.  Unfortunately, when  viewing the page on the iPhone and Internet Explorer 6 (via a VM), I realized that some JavaScript engines do not know how to interpret this format.  A format that is parsed correctly is “mm/dd/yyyy”.  Therefore, if you are passing a date or a date/time value from the server to the browser so that it can be processed by JavaScript, I suggest using “mm/dd/yyyy” to represent the date so that you can be sure that it is parsed correctly across all browsers.  Alternatively, you could send the amount of milliseconds that have passed since January 1, 1970 to the date function because this should always be interpreted correctly.

// Invalid date in some browsers and 
// "Tue Mar 20 2012 20:00:00 GMT-0400 (EDT)" in others
alert(new Date("2012-03-21"));
// eg. "Wed Mar 21 2012 00:00:00 GMT-0400 (EDT)"
alert(new Date("03/21/2012"));
// eg. "Wed Mar 21 2012 00:00:00 GMT-0400 (EDT)"
alert(new Date(1332302400000));

Keep in mind that the above code was run on 19 of March 2012 in Pennsylvania in Google Chrome 17.0.963.79.

Categories: BlogJavaScriptJScript

Leave a Reply

Your email address will not be published. Required fields are marked *