如何检查 DST夏令时是否有效,如果有效,偏移量?

问题描述

代码使用在标准时间与夏令时 (DST) 期间getTimezoneOffset返回 更大 值的事实。因此,它确定标准时间期间的预期输出,并比较给定日期的输出是否相同(标准)或更少(DST)。

请注意,对于 UTC 以西getTimezoneOffset的区域,返回 数分钟数,通常表示为 负数 (因为它们“落后于”UTC)。例如,洛杉矶是 *

Date.prototype.stdTimezoneOffset = function () {
    var jan = new Date(this.getFullYear(), 0, 1);
    var jul = new Date(this.getFullYear(), 6, 1);
    return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
}

Date.prototype.isDstObserved = function () {
    return this.getTimezoneOffset() < this.stdTimezoneOffset();
}

var today = new Date();
if (today.isDstObserved()) { 
    alert ("Daylight saving time!");
}

解决方法

这是我需要的一些 JS 代码:

var secDiff = Math.abs(Math.round((utc_date-this.premiere_date)/1000));
this.years = this.calculateUnit(secDiff,(86400*365));
this.days = this.calculateUnit(secDiff-(this.years*(86400*365)),86400);
this.hours = this.calculateUnit((secDiff-(this.years*(86400*365))-(this.days*86400)),3600);
this.minutes = this.calculateUnit((secDiff-(this.years*(86400*365))-(this.days*86400)-(this.hours*3600)),60);
this.seconds = this.calculateUnit((secDiff-(this.years*(86400*365))-(this.days*86400)-(this.hours*3600)-(this.minutes*60)),1);

我想在“之前”中获取日期时间,但如果 DST 正在使用,那么日期会延迟 1 小时。我不知道如何检查 DST 是否生效。

我怎么知道夏令时开始和结束的时间?