4

Here is the code from my debugger:

new Date('05-20-2015').toString()
"Wed May 20 2015 00:00:00 GMT-0500 (Central Daylight Time)"
new Date('2015-05-20').toString()
"Tue May 19 2015 19:00:00 GMT-0500 (Central Daylight Time)"

I was expecting the same results, but why are they different?

newman
  • 6,841
  • 21
  • 79
  • 126
  • My browser (Firefox) says "Invalid date" for the first one. Which browser are you using? – Greg Hewgill May 21 '15 at 03:23
  • @GregHewgill Interesting, Firefox says it's invalid for me as well (despite the fact that I'm in the US, where that format is unfortunately the norm). Chrome is fine with it. – ajp15243 May 21 '15 at 03:24
  • @GregHewgill It will depend on the locale/timezone used.... for me it is working – Arun P Johny May 21 '15 at 03:26
  • @ArunPJohny: ...and that's an excellent reason to *not* rely on such behaviour! Gotta love that JS code that will work on your machine, but break for most of the rest of the world. – Greg Hewgill May 21 '15 at 03:28
  • I got that from Visual Studio debugger in my node.js project. – newman May 21 '15 at 03:35
  • you should only parse the date formats that Date() itself provides, gmt, iso, etc. nothing else is guaranteed to work anywhere. – dandavis May 21 '15 at 04:08

2 Answers2

2

I think the difference is how the value is parsed.

new Date('05-20-2015').toString();//parse the date using local time zone
new Date('2015-05-20').toString();//parses date as if it is in GMT

As you can see there is a difference of 5hrs between the given values

Date.parse()

Given a string representing a time, parse() returns the time value. It accepts the RFC2822 / IETF date syntax (RFC2822 Section 3.3), e.g. "Mon, 25 Dec 1995 13:30:00 GMT". It understands the continental US time zone abbreviations, but for general use, use a time zone offset, for example, "Mon, 25 Dec 1995 13:30:00 +0430" (4 hours, 30 minutes east of the Greenwich meridian). If a time zone is not specified and the string is in an ISO format recognized by ES5, UTC is assumed. GMT and UTC are considered equivalent. The local time zone is used to interpret arguments in RFC2822 Section 3.3 format (or any format not recognized as ISO 8601 in ES5) that do not contain time zone information.

Here 2015-05-20 is in ISO8601 format, SO it is processed using UTC timezone.

If you want the same result you can pass the timezone like new Date('2015-05-20 GMT-0500')

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
1

ISO format, (ex YYYY-MM-DD is the short form) will be treated as UTC timezone.

Other date strings will be treated as local timezone.

MDN source

Related

Manually setting timezone

var d1 = new Date('05-20-2015'); // local timezone

var d2 = new Date('2015-05-20'); // UTC timezone
d2.setUTCMinutes(d2.getTimezoneOffset()); // need to set to local timezone

console.log(d1.toString() === d2.toString()); // true
Community
  • 1
  • 1
Miguel Mota
  • 20,135
  • 5
  • 45
  • 64
  • Well, it doesn't make sense because I believe some countries use 'YYYY-MM-DD' as local format as well. – newman May 21 '15 at 03:37