Moment.js - How To Detect Daylight Savings Time And Add One Day

后端 未结 2 630
星月不相逢
星月不相逢 2020-12-05 17:40

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 Mont

相关标签:
2条回答
  • 2020-12-05 18:09

    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.

    0 讨论(0)
  • 2020-12-05 18:16

    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;
    
    0 讨论(0)
提交回复
热议问题