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

前端 未结 14 771
栀梦
栀梦 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:29

    I recently needed to create a date string with UTC and DST, and based on Sheldon's answer I put this together:

    Date.prototype.getTimezone = function(showDST) {
        var jan = new Date(this.getFullYear(), 0, 1);
        var jul = new Date(this.getFullYear(), 6, 1);
    
        var utcOffset = new Date().getTimezoneOffset() / 60 * -1;
        var dstOffset = (jan.getTimezoneOffset() - jul.getTimezoneOffset()) / 60;
    
        var utc = "UTC" + utcOffset.getSign() + (utcOffset * 100).preFixed(1000);
        var dst = "DST" + dstOffset.getSign() + (dstOffset * 100).preFixed(1000);
    
        if (showDST) {
            return utc + " (" + dst + ")";
        }
    
        return utc;
    }
    Number.prototype.preFixed = function (preCeiling) {
        var num = parseInt(this, 10);
        if (preCeiling && num < preCeiling) {
            num = Math.abs(num);
            var numLength		 = num.toString().length;
            var preCeilingLength = preCeiling.toString().length;
            var preOffset		 = preCeilingLength - numLength;
            for (var i = 0; i < preOffset; i++) {
                num = "0" + num;
            }
        }
        return num;
    }
    Number.prototype.getSign = function () {
        var num	 = parseInt(this, 10);
        var sign = "+";
        if (num < 0) {
            sign = "-";
        }
        return sign;
    }
    
    document.body.innerHTML += new Date().getTimezone() + "<br>";
    document.body.innerHTML += new Date().getTimezone(true);
    <p>Output for Turkey (UTC+0200) and currently in DST: &nbsp; UTC+0300 (DST+0100)</p>
    <hr>

    0 讨论(0)
  • 2020-11-22 10:32

    Based on Matt Johanson's comment on the solution provided by Sheldon Griffin I created the following code:

        Date.prototype.stdTimezoneOffset = function() {
            var fy=this.getFullYear();
            if (!Date.prototype.stdTimezoneOffset.cache.hasOwnProperty(fy)) {
    
                var maxOffset = new Date(fy, 0, 1).getTimezoneOffset();
                var monthsTestOrder=[6,7,5,8,4,9,3,10,2,11,1];
    
                for(var mi=0;mi<12;mi++) {
                    var offset=new Date(fy, monthsTestOrder[mi], 1).getTimezoneOffset();
                    if (offset!=maxOffset) { 
                        maxOffset=Math.max(maxOffset,offset);
                        break;
                    }
                }
                Date.prototype.stdTimezoneOffset.cache[fy]=maxOffset;
            }
            return Date.prototype.stdTimezoneOffset.cache[fy];
        };
    
        Date.prototype.stdTimezoneOffset.cache={};
    
        Date.prototype.isDST = function() {
            return this.getTimezoneOffset() < this.stdTimezoneOffset(); 
        };
    

    It tries to get the best of all worlds taking into account all the comments and previously suggested answers and specifically it:

    1) Caches the result for per year stdTimezoneOffset so that you don't need to recalculate it when testing multiple dates in the same year.

    2) It does not assume that DST (if it exists at all) is necessarily in July, and will work even if it will at some point and some place be any month. However Performance-wise it will work faster if indeed July (or near by months) are indeed DST.

    3) Worse case it will compare the getTimezoneOffset of the first of each month. [and do that Once per tested year].

    The assumption it does still makes is that the if there is DST period is larger then a single month.

    If someone wants to remove that assumption he can change loop into something more like whats in the solutin provided by Aaron Cole - but I would still jump half a year ahead and break out of the loop when two different offsets are found]

    0 讨论(0)
提交回复
热议问题