How to check if DST (Daylight Saving Time) is in effect, and if so, the offset?

前端 未结 14 778
栀梦
栀梦 2020-11-22 09:46

This is a bit of my JS code for which this is needed:

var secDiff = Math.abs(Math.round((utc_date-this.premiere_date)/1000));
this.years = this.calculateUnit         


        
14条回答
  •  逝去的感伤
    2020-11-22 10:11

    I was faced with this same problem today but since our daylight saving starts and stops at differing times from the USA (at least from my understanding), I used a slightly different route..

    var arr = [];
    for (var i = 0; i < 365; i++) {
     var d = new Date();
     d.setDate(i);
     newoffset = d.getTimezoneOffset();
     arr.push(newoffset);
    }
    DST = Math.min.apply(null, arr);
    nonDST = Math.max.apply(null, arr);
    

    Then you simply compare the current timezone offset with DST and nonDST to see which one matches.

提交回复
热议问题