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

前端 未结 14 1075
栀梦
栀梦 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've found that using the Moment.js library with some of the concepts described here (comparing Jan to June) works very well.

    This simple function will return whether the timezone that the user is in observes Daylight Saving Time:

    function HasDST() {
        return moment([2017, 1, 1]).isDST() != moment([2017, 6, 1]).isDST();
    }
    

    A simple way to check that this works (on Windows) is to change your timezone to a non DST zone, for example Arizona will return false, whereas EST or PST will return true.

提交回复
热议问题