46

I need to create Date Objects from strings of Date data for every hour of every day since the year 2000.

The strings look like this for every hour, in a Month/Day/Year Hour format...

"04/02/2000 01", "04/02/2000 02", "04/02/2000 03" ...all the way to... "04/02/2000 24"

Now, I have the following code, which works fine except for on days with Daylight Savings Time...

  // Split At Space
var splitDate = "04/02/2000 24".split(/[ ]+/);
var hour = splitDate[1];
var day  = splitDate[0];
  // Split At Slashes
var dayArray = day.split("/");
if (hour === "24") {
       // Months are zero-based, so subtract 1 from the month
     date = new Date(Date.UTC( dayArray[2], parseInt(dayArray[0] - 1), dayArray[1], 0, 0, 0 ));
     date.setDate(date.getDate() + 1);
} else {
     // Months and Hours are zero-based, so subtract 1 from each
     date = new Date(Date.UTC( dayArray[2], parseInt(dayArray[0] - 1), dayArray[1], hour, 0, 0 ));
};

On days with daylight savings time, like 04/02/2000 adding a day does not work if the hour is 24. Instead, it just returns Sun, 02 Apr 2000 23:00:00 GMT

With Moment.js, is it possible to detect a DST day and get this code to work correctly?

ac360
  • 7,735
  • 13
  • 52
  • 91

2 Answers2

60

To detect DST, use the .isDST() method: http://momentjs.com/docs/#/query/is-daylight-saving-time/

moment([2011, 2, 12]).isDST(); // false, March 12 2011 is not DST
moment([2011, 2, 14]).isDST(); // true, March 14 2011 is DST

Using this test, you should be able to determine how to modify your program's behavior accordingly.

adamb
  • 4,815
  • 1
  • 25
  • 40
  • 12
    It might be worth mentioning that isDST() depends on the country and region. The examples given will both return false for Belgium. http://www.timeanddate.com/time/dst/2011.html – Christophe Herreman Apr 11 '14 at 15:40
  • 1
    Even parts of the United States it would return false on, as there are small pockets in the U.S. that don't use DST. In fact not to many countries in general in the world use DST. – chris Sep 04 '14 at 17:31
  • 1
    It is false. DST is widely used in North America and Europe, as well as in other countries. See this link from Wikipedia: https://en.wikipedia.org/wiki/Daylight_saving_time_by_country – WesternGun Dec 21 '17 at 09:11
  • Is this code returning whether it is DST of whether is the date that the DST kicks in/hours change? – Mathieu Brouwers Apr 02 '19 at 13:51
  • 3
    I tried this code and they are both returning false. I am running it from Florida, which has DST. Did i miss something? Do i need to set region? – Logan_B Apr 05 '19 at 16:26
1

Here's how I made a little checker:

var curDst = dtdate.isDST()
var prevDst = moment(dtdate).clone().subtract(1, "day").isDST();
var nextDst = moment(dtdate).clone().add(1, "day").isDST();
var isDstChangeDate = (curDst !== nextDst) === true || (curDst === prevDst) !== true;
red
  • 1,980
  • 1
  • 15
  • 26
  • 1
    That won't work for all timezone. I.e with `Iran` timezone, `2018-03-22` is the real DST change day but with that logic, the `2018-03-21` would be the DST change day... – dguay Nov 28 '18 at 19:02
  • I've actually revised the checker in my own code. I updated the answer. – red Dec 18 '18 at 09:11
  • @red, your code returns true for both `2018-03-22` and `2018-03-23` for `Iran` timezone – Saeed Zhiany Sep 29 '21 at 07:29