如何检查 DST(夏令时)是否有效,如果有效,偏移量? [英] How to check if DST (Daylight Saving Time) is in effect, and if so, the offset?

查看:38
本文介绍了如何检查 DST(夏令时)是否有效,如果有效,偏移量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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(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);

我想在ago"中获取日期时间,但如果 DST 正在使用中,那么日期会延迟 1 小时.我不知道如何检查夏令时是否生效.

I want to get the datetime in "ago", but if the DST is in use then the dates are off by 1 hour. I don't know how to check if the DST is in effect or not.

我如何知道夏令时的开始和结束时间?

How can I know when the daylight saving starts and ends?

推荐答案

此代码使用了这样一个事实:getTimezoneOffset 在标准时间与夏令时 (夏令时).因此,它确定标准时间期间的预期输出,并比较给定日期的输出是否相同(标准)或更少(DST).

This code uses the fact that getTimezoneOffset returns a greater value during Standard Time versus Daylight Saving Time (DST). Thus it determines the expected output during Standard Time, and it compares whether the output of the given date the same (Standard) or less (DST).

请注意,getTimezoneOffset 返回 UTC west 区域的 分钟数,通常表示为 negative 小时(因为它们落后于"UTC).例如,洛杉矶是 UTC-8h 标准,UTC-7h DST.getTimezoneOffset 返回 12 月(冬季,标准时间)的 480(正 480 分钟),而不是 -480.它返回东半球的数字(例如冬季悉尼的-600,尽管这是超前"(UTC+10h).

Note that getTimezoneOffset returns positive numbers of minutes for zones west of UTC, which are usually stated as negative hours (since they're "behind" UTC). For example, Los Angeles is UTC–8h Standard, UTC-7h DST. getTimezoneOffset returns 480 (positive 480 minutes) in December (winter, Standard Time), rather than -480. It returns negative numbers for the Eastern Hemisphere (such -600 for Sydney in winter, despite this being "ahead" (UTC+10h).

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!");
}

这篇关于如何检查 DST(夏令时)是否有效,如果有效,偏移量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆