tl;dr - When I try to create a new Date object with a YYYY-MM-DD format date string, it gives me an incorrect date (yesterday). Why?
I've written the following test code to help me demonstrate the problem I'm perceiving:
var dateConfig = {weekday: "long", year: "numeric", month: "long", day: "numeric"},
dates = [
"01/21/2014",
"01-21-2014",
"2014/01/21",
"2014-01-21"
];
for (var i = 0; i < dates.length; ++ i) {
var date = new Date(dates[i]);
console.log(date.toLocaleDateString("en-US", dateConfig));
}
Link to see for yourself: http://s.codepen.io/AdrianTP/fullpage/prKyf
Chrome 31.0.1650.63 m
returns the following in the console:
Tuesday, January 21, 2014
Tuesday, January 21, 2014
Tuesday, January 21, 2014
Monday, January 20, 2014
Firefox 26.0
returns the following in the console:
"Tuesday, January 21, 2014"
"Invalid Date"
"Tuesday, January 21, 2014"
"Monday, January 20, 2014"
Even Internet Explorer 8 gets most of it right, returning the following in the console:
"Tuesday, January 21, 2014"
"Tuesday, January 21, 2014"
"Tuesday, January 21, 2014"
"NaN"
In short, I am aware that date handling between browsers is inconsistent (dates[2] in Chrome and Firefox differ, and dates[3] just outright breaks in IE 8), but that is not my question.
My question is:
Why would Chrome and Firefox return yesterday's date for a YYYY-MM-DD formatted date string specifying today's date, when it works fine with slashes?
Another question:
Is this a known issue?
I have not encountered it before, and was unable to find any documentation of the issue, nor documentation of the Date() object which would indicate to me that such string-transformation would occur so regularly-irregular. Does anyone out there have experience with this and maybe an explanation or a link to one that I haven't found? I could just be using the wrong search terms here...