6

I'm trying to compare a GMT time offset from the operating system to a GMT time offset from Javascript's Date.getTimezoneOffset(). The problem is windows gives an offset based on EST while javascript gives an offset based on EDT. There is an hour difference between these two. Does anyone know how to make Javascript use the Standard Times like Windows? Thank you.

SquidScareMe
  • 3,108
  • 2
  • 24
  • 37

2 Answers2

13

Note, that the first posted answer does only work in a half of all cases, thus not work at all on average.

First january is known not to be in daylight saving time only in the northern hemisphere. However that's only half of the world.

var jan = new Date( 2009, 0, 1, 2, 0, 0 ), jul = new Date( 2009, 6, 1, 2, 0, 0 );
var offset = ( jan.getTime() % 24 * 60 * 60 * 1000 ) > 
             ( jul.getTime() % 24 * 60 * 60 * 1000 )
             ?jan.getTimezoneOffset() : jul.getTimezoneOffset();
Steffen Heil
  • 4,286
  • 3
  • 32
  • 35
  • 2
    Not that alienating any group is ok, but note that the northern "half of the world" comprises 90% of the world population. – hyperslug Mar 13 '11 at 00:48
  • 1
    @hyperslug - if SquidScareMe is after a northern hemisphere solution only, then that's fine for him. However, SO *should* provide the *best* solution, and Steffen's solution is technically more correct (the best kind of correct!). – Andrew Jun 25 '12 at 05:39
  • In order to make this work, I first get the current month and then change it there where you see 6 (Jul). – MPaulo Nov 04 '12 at 04:50
  • 1
    @Andrew - I totally agree, not only is a Northern Hemisphere-only solution borderline offensive to Southern Hemisphere residents, it's downright sloppy to walk away from your code knowing it will fail miserably for 10% of the global population. – Michael12345 Nov 01 '13 at 02:08
  • Shouldn't it be >= on the second line when comparing jan and jul? I'm UTC+1, and I got -120 as offset instead of -60 from this since have DST and UTC+2 during summer which means I get the time zone offset from July which is wrong. – Eirik H Nov 11 '13 at 10:58
5

Why not call getTimezoneOffset on a date where you know daylight saving time is not in force?

javascript:alert(new Date('1/1/2009').getTimezoneOffset())

That will give 300 (5 hours).

jhurshman
  • 5,861
  • 2
  • 26
  • 16