3

I have this simple Javascript code

var d = new Date(2011, 9, 8);
alert(d); // Show: Fri Oct 7 23:00:00 UTC-0400 2011

Important: My Time Zone is "Santiago de Chile", and I set computer clock to: 2-Oct-2011.

Alert show that Day is 7!!... why? How I can get it right? (Problem is only in this day)

Victor Sanchez
  • 583
  • 9
  • 28
  • Are your really sure that if you use `new Date(2011, 9, 7)` it also displays as `Fri Oct 7 …`? Otherwise it would be a timezone problem. – Marcel Korpel May 02 '11 at 17:12
  • 100% sure... set Time Zone to "Santiago de Chile" to see problem – Victor Sanchez May 02 '11 at 17:20
  • Ah! And if you set it to a date *after* 8-Oct-2011? Moreover, on what system do you test this? – Marcel Korpel May 02 '11 at 17:23
  • I test it in Windows (xp and 2003). Problem is ONLY this day. This day goberment chage date to use sumner (or winter... In don't know) day light time. – Victor Sanchez May 02 '11 at 17:28
  • Strange. Can you try to run the script from [this question](http://stackoverflow.com/questions/4018320/javascript-date-objects-and-daylight-savings-time) (with `Date(2011, 9, 7, 20, 0, 0, 0)` to start with, of course) and include its output to your question? – Marcel Korpel May 02 '11 at 17:34
  • Of course... this is the output: 23 Fri Oct 7 23:00:00 UTC-0400 2011 23 Fri Oct 7 23:00:00 UTC-0400 2011 23 Fri Oct 7 23:00:00 UTC-0400 2011 23 Fri Oct 7 23:00:00 UTC-0400 2011 23 Fri Oct 7 23:00:00 UTC-0400 2011 23 Fri Oct 7 23:00:00 UTC-0400 2011 23 Fri Oct 7 23:00:00 UTC-0400 2011 23 Fri Oct 7 23:00:00 UTC-0400 2011 23 Fri Oct 7 23:00:00 UTC-0400 2011 23 Fri Oct 7 23:00:00 UTC-0400 2011 23 Fri Oct 7 23:00:00 UTC-0400 2011 23 Fri Oct 7 23:00:00 UTC-0400 2011 – Victor Sanchez May 02 '11 at 17:42
  • Hmm, is DST change in your timezone exactly at midnight instead of 3:00 AM (as it is in my country)? – Marcel Korpel May 02 '11 at 17:43
  • Yes... a very rare Chile stuff... :( – Victor Sanchez May 02 '11 at 17:46
  • Cannot duplicate this issue locally - are you sure your XP is entirely up-to-date? http://imagebin.org/151339 – TML May 02 '11 at 18:35
  • @TML: it's an issue that always happens when summer time starts in your region, see my answer below on how to reproduce. – Marcel Korpel May 02 '11 at 18:55

1 Answers1

0

According to this page DST change in Chile is always at the second full weekend of October and starts at midnight (lets disregard the extended DST of this year, as it's very likely your computer doesn't know about it).

That means that, as is also shown using the script I linked to earlier, at your timezone 8 Oct 2011 midnight can't be represented in UTC-0300, so it automatically switches to UTC-0400 to represent the same time.

I can reproduce your issue in my timezone (Amsterdam, UTC+1/2h) by asking for Sun 27 March 2011, 2:00 AM (with current timezone CEST=UTC+0200) (at which time summer time (CEST) starts and the clock is adjusted from UTC+0100 to UTC+0200) (remember this is exactly the opposite on the northern hemisphere))

new Date(2011, 2, 27, 2, 0, 0, 0);

which effectively can't be represented in UTC+0200, so the system chooses UTC+0100 and outputs

Sun Mar 27 2011 01:00:00 GMT+0100 (CET)

So, it's the same time in Unix time, only represented differently in your local timezone.

And yes, I know 8 October is a Saturday and DST change starts at Sunday, midnight, but this is the closest I can get.


Update: I can now reproduce your issue by setting the timezone of a WinXP machine to Santiago and let the OS automatically adjust for DST and alerting new Date(2011, 9, 9) (which is a Sunday and is shown as a Saturday; there must be something strange with your settings). Of course you can't control these settings on the client.

To work around this issue, I have to know what you exactly need: if you merely want some date/time display that doesn't take timezone and DST into account, just use UTC, like:

var d = new Date(Date.UTC(2011, 9, 9));
alert(d.toUTCString()); // Shows: Sun, 9 Oct 2011 00:00:00 UTC

To show/calculate with the individual parts of the Date object, use the corresponding UTC methods.

Community
  • 1
  • 1
Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80