5

I noticed something weird with timezones and Javascript Date object.

Trying this on a Linux box:

$ TZ='America/Sao_Paulo' js 
js> new Date(2012, 9, 21, 0, 0, 0).toString() 
"Sat Oct 20 2012 23:00:00 GMT-0300 (BRST)"

I found it impossible to get an object that represents the 21th of october 2012. Every attemps to get a Date between 00:00 and 01:00 that day results in a date the day before between 11:00 PM and 00:00.

(Windows user may change their timezone to Brasilia/GMT-03 to experience the same behavior)

Adding one hour (or one day) to such a date object results in getting back the same date.

I guess it has to do with daylight saving time which occurs on that specific date in Brazil, however I absolutely don't know how to deal with that.

I have a loop that iterates over the days of the month (for a calendar application) and it obviously loops forever in that case when reaching the 20th of october 2012.

There must be something I'm doing wrong, but I don't know how to deal with such a case, as I guess it could also happen for other timezones.

What is the recommended way of dealing with Date in Javascript to ensure that such timezone/daylight saving time related problems do not occur ?

ereOn
  • 53,676
  • 39
  • 161
  • 238

1 Answers1

0

This is an absolutely correct behavior. Due to the DST midnight simply does not exist in local time in Brazil on October 21st. If you just need to build a calendar you can just use new Date(2012, 9, 21, 0, 0, 0).toUTCString() instead of new Date(2012, 9, 21, 0, 0, 0).toString() and other UTC methods.

For example to create an UTC date you can use new Date(Date.UTC(2012, 9, 21, 0, 0, 0));.

bjornd
  • 22,397
  • 4
  • 57
  • 73
  • Thanks for your answer. However my problem is not getting the string representation of the date, but getting a validd object that represents this exact moment. – ereOn Oct 03 '12 at 09:40
  • 1
    Then just use UTC method: `new Date(Date.UTC(2012, 9, 21, 0, 0, 0));` – bjornd Oct 03 '12 at 10:16
  • That works better indeed. Could you please edit your answer to make it more obvious what the real solution is ? I will be glad to accept it then :) – ereOn Oct 03 '12 at 10:19
  • I edited it already, think it answers your question better now. – bjornd Oct 03 '12 at 10:23
  • I don't believe this is correct. It's a transition *forward* - so there's a gap in local times, *not* an ambiguity. (You get ambiguity when local time moves backward, so the same local time occurs twice.) In fact, midnight simply didn't exist in local time in Brazil on October 21st. – Jon Skeet Oct 24 '12 at 11:33
  • @Jo In fact the OP asked not about the reason of his problem but for the solution. I edited the answer to make it abolutely correct. – bjornd Oct 24 '12 at 13:23
  • @bjornd: You're still claiming it's correct behaviour for Javascript to give a result of 11pm. It's not. There are various options which are reasonable, but none of them is to go *back* an hour. Would you think it okay to return 10pm? 11:35pm? – Jon Skeet Oct 24 '12 at 13:51