Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does momentjs isDST() return wrong time when zone() is used

I'm trying to check for isDST() (returns true or false if daylight saving time is active). It works fine if I use the current date time- for example var isdst = moment().isDST() returns true for my timezone. However, what I want to do is first set the timezone offset and then check to see if daylight saving time is active in that timezone. I have the following code below.

var isdst = moment('2014-03-28').zone('+01:00');
console.log('daylight savings for +0100 is ' + isdst); //returns true when it should return false

If you look at the DST time for timezone +0100 (European Union countries) it does not come into effect until March 30, 2014. However, the code above returns true for March 28 when in fact should not (until 2 days later). You can check the DST for different countries here http://www.worldtimezone.com/daylight.html

I did some more testing and it seems that the code is taking into account my own timezone (US Eastern Standard) when it runs. If you see the site above, the Eastern Standard daylight saving begins on March 9, 2014. If I test the code a couple days previous to March 9 it returns false (i.e. March 8). If I test it with March 11 it returns true. This tells me that it is not taking into account the zone("+0100") and somehow using my timezone...why? How can I set the timezone for a momentjs date? I looked in the documentation (http://momentjs.com/docs/) and it says that this is the correct way, but it is not working for me.

like image 709
user1142130 Avatar asked Jun 02 '14 14:06

user1142130


1 Answers

A time zone is not the same as an offset. UTC+01:00 applies to many different time zones, some use daylight saving time and some do not. Those that use it do not all apply it at the same time either. See "Time zone != Offset" in the timezone tag wiki.

Moment's "zone" function is really for specifying an offset. It will still use your own local DST rules.

If you want to know about DST rules for another time zone, you have to specify that zone using the moment-timezone add-on. For example:

var tz = 'Europe/Paris';           // or whatever your time zone is
var dt = '2014-05-14 12:34:56';    // or whatever date/time you're working with
moment.tz(dt,tz).isDST()           // returns true in this case
like image 96
Matt Johnson-Pint Avatar answered Nov 10 '22 01:11

Matt Johnson-Pint