1

When I construct a date object from a string, I am getting confusing results. It seems as if the time is chosen arbitrarily (but repeatably) if I don't specify it.

var d1=new Date("2013-10-9"), d2=new Date("2013-10-10");
output  = d1+' '+d1.toUTCString()+'<br>\n';
output += d2+' '+d2.toUTCString()+'<br>\n';

Chromium 20.0...

Wed Oct 09 2013 00:00:00 GMT-0600 (MDT) Wed, 09 Oct 2013 06:00:00 GMT
Wed Oct 09 2013 18:00:00 GMT-0600 (MDT) Thu, 10 Oct 2013 00:00:00 GMT

Why would Chromium choose a different time on October 10?

By the way, the workaround is here: https://stackoverflow.com/a/744134/86967

Community
  • 1
  • 1
Brent Bradburn
  • 51,587
  • 17
  • 154
  • 173

1 Answers1

2

It has to do with the format of the date string you are using. If you specify 2013-10-09 (notice the extra 0 on the day), then it works as expected. If you use 2 digits for the day and month, then you are following the ECMA spec.

var d1=new Date("2013-10-09"), d2=new Date("2013-10-10");
console.log(d1+' '+d1.toUTCString());
console.log(d2+' '+d2.toUTCString());

Yields:

Tue Oct 08 2013 20:00:00 GMT-0400 (Eastern Daylight Time) Wed, 09 Oct 2013 00:00:00 GMT 
Wed Oct 09 2013 20:00:00 GMT-0400 (Eastern Daylight Time) Thu, 10 Oct 2013 00:00:00 GMT

I believe the code they are using can be found here:

https://github.com/WebKit/webkit/blob/master/Source/WTF/wtf/DateMath.cpp

When you provide an ECMA date, it will use the parseES5DateFromNullTerminatedCharacters method to parse the date, but when you use a non-standard date format it will use the parseDateFromNullTerminatedCharacters method. I am not that familiar with the webkit code, so I could be wrong, but this is based on my reading of the parsing logic.

The standard date format can be found in section 15.9.1.15 of the ECMA Spec.

John Koerner
  • 37,428
  • 8
  • 84
  • 134
  • If you use the ISO standard, it chooses midnight GMT -- otherwise, it chooses midnight local-time? This seems to be the case, but I don't understand why. Starting with the 10th in October, November, and December, the formatting automatically fixes itself. – Brent Bradburn Jul 26 '13 at 03:46
  • 1
    The formatting fixes itself, because you are following the ISO standard of YYYY-MM-DD, in months prior to october, you are probably only using a single digit for the Month (i.e. 2013-9-10). When you hit the tenth of a month that has two digits and you hit a date after the tenth of the month you are now in compliance with the ISO standard and it uses different parsing logic for those dates. – John Koerner Jul 26 '13 at 03:47
  • That is what I meant -- I was just trying to point out the same thing. It seems strange that Javascript would have a different interpretation if the zero is excluded since there isn't a way to exclude the zero on days such as 2013-12-31. – Brent Bradburn Jul 26 '13 at 03:53
  • @nobar I updated my answer to include a link to the code used by webkit. Based on my understanding they use different methods based on the format of the string you pass in. It is not strange at all. You are passing it an arbitrary string and it is trying to figure out the date for you. If you pass in a string that follows the standard, it can easily handle that. If you don't, it has to try and figure it out and that can result in different logic getting executed. – John Koerner Jul 26 '13 at 03:56