3

Given: a date and its time zone in JS (in browser).

What is needed: Need to know whether the given date falls in the Daylight saving period of the given time zone or not.

One possible JS library to use is MomentJS. Is it possible to get Daylight saving moments for a given timezone?

In case, this solves my question, I tried to play with momentJS timezone here, but how do i interpret the following output:

moment.tz.add({
    "zones": {
        "CET": [
            "1 C-Eur CE%sT"
        ],
        "Etc/GMT": [
            "0 - GMT"
        ],
        "Etc/UTC": [
            "0 - UTC"
        ]
    },
    "rules": {
        "C-Eur": [
            "1916 1916 3 30 7 23 0 1 S",
            "1916 1916 9 1 7 1 0 0",
            "1917 1918 3 15 1 2 2 1 S",
            "1917 1918 8 15 1 2 2 0",
            "1940 1940 3 1 7 2 2 1 S",
            "1942 1942 10 2 7 2 2 0",
            "1943 1943 2 29 7 2 2 1 S",
            "1943 1943 9 4 7 2 2 0",
            "1944 1945 3 1 1 2 2 1 S",
            "1944 1944 9 2 7 2 2 0",
            "1945 1945 8 16 7 2 2 0",
            "1977 1980 3 1 0 2 2 1 S",
            "1977 1977 8 0 8 2 2 0",
            "1978 1978 9 1 7 2 2 0",
            "1979 1995 8 0 8 2 2 0",
            "1981 9999 2 0 8 2 2 1 S",
            "1996 9999 9 0 8 2 2 0"
        ]
    },
    "links": {}
});
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
monish001
  • 671
  • 2
  • 8
  • 20

1 Answers1

3

You can just do this:

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
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Thanks Matt for the response. This solves my requirement. On a separate note if you know, does momentJS works consistently across browsers (IE 8+, firefox, chrome, safari)? I am asking this after reading an answer from http://stackoverflow.com/questions/2532729/daylight-saving-time-and-time-zone-best-practices?rq=1 - "in some browsers the calculation for when to apply daylight savings seemed to be different than in others." – monish001 May 15 '14 at 18:05
  • 1
    This will work consistently because it doesn't rely on the `Date` object to provide the time zone information. Make sure you include the moment-timezone data for the time zones you are interested in. moment-timezone will use that data to make the determination. – Matt Johnson-Pint May 15 '14 at 18:08
  • FYI - the Jan/Jul test has been proven many other times. While not all time zones transition at the same points, none of them transition anywhere close to Jan 1 or Jul 1. – Matt Johnson-Pint May 15 '14 at 18:10
  • Also - I'm a contributor to moment.js and I've been falling behind on moment-timezone issues. I'll be sure to get this one taken care of soon so `isDSTShifted` should eventually just work as designed. – Matt Johnson-Pint May 15 '14 at 18:11
  • Matt, Though your answer solves what i wanted. I am wondering if it is possible to get daylight saving transition start and end moments for given year and timezone? (This is what my original question was. :) – monish001 May 20 '14 at 08:56
  • Not directly, no. You'd have to write a function for that. Moment doesn't expose it directly. – Matt Johnson-Pint May 21 '14 at 06:08
  • Thanks again Matt for your previous response. The function isDst(dt, tz) that you gave is just awesome but I have one concern. Are there any timezone(s) that has daylight periods for some years and not for all years? If yes, this will break the isDST(dt, tz) due to hard-coding for year 2014. – monish001 May 23 '14 at 13:46
  • @Monish - Silly me - using the correct moment function and it works fine. – Matt Johnson-Pint Jun 02 '14 at 17:38
  • I am using below mentioned versions for momentJS. All the following JS statements returning false instead true. moment.tz('2014-06-14', 'CST6CDT').isDST(). moment.tz('2014-06-14', 'EST5EDT').isDST(). moment.tz('2014-06-14', 'PST8PDT').isDST(). Am I missing something? Version used: MomentJS timezone 0.0.3 https://github.com/moment/moment-timezone/tree/27a88581c0d44c557d5122cde9946043fa1da94d. MomentJS 2.5.1 https://github.com/moment/moment/tree/abc3a14b2ed7c9f4ff420395f3e1f198459528d8. Let me know in case you feel that there is more appropriate forum for these discussions. – monish001 Jun 13 '14 at 08:43
  • The [moment-timezone issue tracker](https://github.com/moment/moment-timezone/issues) is the best place to report a bug. But in general, try to avoid those particular time zones. They stem from backwards compatibility with POSIX values (see [the timezone tag wiki](http://stackoverflow.com/tags/timezone/info) near the bottom). Instead, use `America/New_York`, `America/Chicago`, `America/Denver`, `America/Los_Angeles`, etc. – Matt Johnson-Pint Jun 13 '14 at 16:42