Annoying javascript timezone adjustment issue

Everything is fine, try this:

new Date(Date.parse("2011-10-02T23:00+02:00")).getUTCHours()  //21

The date is parsed correctly (taking the timezone into account as expected). However when you simply print Date.toString() it shows the date in current browser timezone (one of the sins of Java Date object shamelessly copied to JavaScript…)

If you stick to getUTC*() family of methods you will get correct values (like in example above). The ordinary get*() methods are always affected by browser timezone (and not the timezone from the date you parsed, which is lost), hence often useless.

Another example: the 2011-10-03 02:00+03:00 is actually 23:00 on 2nd of October. But when you parse it (my current browser time zone is +0200 (CEST)):

new Date(Date.parse("2011-10-03T02:00+03:00"))  //Oct 03 01:00:00 GMT+0200

However current day of month in UTC is:

new Date(Date.parse("2011-10-03T02:00+03:00")).getUTCDate()  //2 (2nd of Oct)

Leave a Comment