Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript new Date(dateStr) Giving Yesterday's Date in Certain Formats [duplicate]

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...

like image 258
Adrian Avatar asked Jan 21 '14 16:01

Adrian


People also ask

How can I get yesterday's date from a new date?

We use getDate() function to fetch the current date from the date object and subtract one day from it using the setDate() function which sets yesterday's date onto the date object.

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 does new date () return in JavaScript?

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


1 Answers

The JavaScript standard stipulates that the "official" supported format is ISO 8601, which looks like YYYY-MM-DDTHH:mm:ss.sssZ. (Z is the time zone offset specified as “Z” (for UTC) or either “+” or “-” followed by a time expression HH:mm.) Though the format does include a time zone offset, Firefox and Chrome (and I think IE) don't pay attention, and always interpret ISO 8601 dates as UTC.

Any implementation can accept other formats too, and the browsers aren't in sync at present. Firefox will accept RFC 2822 dates, other browsers don't.

like image 127
Pointy Avatar answered Oct 02 '22 14:10

Pointy