Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What the heck is IE new Date(string) doing?

Ok so QA gave me this bug where if a date had a single character starting the month, day or year part of the date (formatted MM/dd/yyyy), that (ONLY in IE) it would parse the date but change it.. So digging around and sure enough its being VERY weird..

This is some sample code of what I am talking about in IE

$("#dates").append("<li>04/30/2012 = " + new Date("04/30/2012").toString()+"</li>");
$("#dates").append("<li>a04/30/2012 = " + new Date("a04/30/2012").toString() +"</li>");
$("#dates").append("<li>b04/30/2012 = " + new Date("b04/30/2012").toString() +"</li>");
$("#dates").append("<li>c04/30/2012 = " + new Date("c04/30/2012").toString() +"</li>");
$("#dates").append("<li>d04/30/2012 = " + new Date("d04/30/2012").toString() +"</li>");
$("#dates").append("<li>e04/30/2012 = " + new Date("e04/30/2012").toString() +"</li>");
$("#dates").append("<li>04/a30/2012 = " + new Date("04/a30/2012").toString() +"</li>");
$("#dates").append("<li>04/b30/2012 = " + new Date("04/b30/2012").toString() +"</li>");
$("#dates").append("<li>04/c30/2012 = " + new Date("04/c30/2012").toString() +"</li>");
$("#dates").append("<li>04/d30/2012 = " + new Date("04/d30/2012").toString() +"</li>");
$("#dates").append("<li>04/e30/2012 = " + new Date("04/e30/2012").toString() +"</li>");
$("#dates").append("<li>04/30/a2012 = " + new Date("04/30/a2012").toString() +"</li>");
$("#dates").append("<li>04/30/b2012 = " + new Date("04/30/b2012").toString() +"</li>");
$("#dates").append("<li>04/30/c2012 = " + new Date("04/30/c2012").toString() +"</li>");
$("#dates").append("<li>04/30/d2012 = " + new Date("04/30/d2012").toString() +"</li>");
$("#dates").append("<li>04/30/e2012 = " + new Date("04/30/e2012").toString() +"</li>");

And here is what IE outputs

•04/30/2012 = Mon Apr 30 2012 00:00:00 GMT-0500 (Central Daylight Time)
•a04/30/2012 = Sun Apr 29 2012 20:00:00 GMT-0500 (Central Daylight Time)
•b04/30/2012 = Sun Apr 29 2012 21:00:00 GMT-0500 (Central Daylight Time)
•c04/30/2012 = Sun Apr 29 2012 22:00:00 GMT-0500 (Central Daylight Time)
•d04/30/2012 = Sun Apr 29 2012 23:00:00 GMT-0500 (Central Daylight Time)
•e04/30/2012 = Mon Apr 30 2012 00:00:00 GMT-0500 (Central Daylight Time)
•04/a30/2012 = Fri Apr 29 -2012 20:00:00 GMT-0500 (Central Daylight Time)
•04/b30/2012 = Fri Apr 29 -2012 21:00:00 GMT-0500 (Central Daylight Time)
•04/c30/2012 = Fri Apr 29 -2012 22:00:00 GMT-0500 (Central Daylight Time)
•04/d30/2012 = Fri Apr 29 -2012 23:00:00 GMT-0500 (Central Daylight Time)
•04/e30/2012 = Sat Apr 30 -2012 00:00:00 GMT-0500 (Central Daylight Time)
•04/30/a2012 = Sun Apr 29 2012 20:00:00 GMT-0500 (Central Daylight Time)
•04/30/b2012 = Sun Apr 29 2012 21:00:00 GMT-0500 (Central Daylight Time)
•04/30/c2012 = Sun Apr 29 2012 22:00:00 GMT-0500 (Central Daylight Time)
•04/30/d2012 = Sun Apr 29 2012 23:00:00 GMT-0500 (Central Daylight Time)
•04/30/e2012 = Mon Apr 30 2012 00:00:00 GMT-0500 (Central Daylight Time)

There is some weird hour add thing going on, except if you add a character to the day part of the format, then it flips the year to BC (from 2012 to -2012???)

We use date pickers so the chance of this happening is pretty limited (but you know QA guys right ;-)... ) So how have others handled this kind of very odd behavior? (FYI: I checked out moment, but it takes a MM/dd/yyyy date and just passes it down to the browsers date parser, so no dice there.

like image 422
Josh Handel Avatar asked Apr 30 '15 21:04

Josh Handel


People also ask

Is new date () a string?

Calling the Date() function (without the new keyword) returns a string representation of the current date and time, exactly as new Date().toString() does.

What is new date () in JavaScript?

The new Date() Constructor In JavaScript, date objects are created with new Date() . new Date() returns a date object with the current date and time.

Does new date () Return current date?

In JavaScript, we can easily get the current date or time by using the new Date() object. By default, it uses our browser's time zone and displays the date as a full text string, such as "Fri Jun 17 2022 10:54:59 GMT+0100 (British Summer Time)" that contains the current date, time, and time zone.

What is the format of new date ()?

By default, when you run new Date() in your terminal, it uses your browser's time zone and displays the date as a full text string, like Fri Jul 02 2021 12:44:45 GMT+0100 (British Summer Time).


1 Answers

It looks like maybe it's being interpreted as military timezone semantics A-Z.

https://www.rfc-editor.org/rfc/rfc822#section-5.2

http://en.wikipedia.org/wiki/List_of_military_time_zones

like image 91
user4749485 Avatar answered Oct 06 '22 23:10

user4749485