首頁 > web前端 > js教程 > 如何確定夏令時 (DST) 是否生效?

如何確定夏令時 (DST) 是否生效?

Mary-Kate Olsen
發布: 2024-11-15 02:10:02
原創
1095 人瀏覽過

How Can I Determine if Daylight Saving Time (DST) is in Effect?

Determining DST and Its Offset

To check if Daylight Saving Time (DST) is in effect, you can utilize the getTimezoneOffset property, which returns a time difference in minutes from UTC. The key difference lies in the convention:

  • Zones west of UTC (usually "behind") display positive offset values. For instance, Los Angeles would show 480 minutes (8 hours positive) during Standard Time and 420 minutes (7 hours positive) during DST.
  • Zones east of UTC display negative offset values.

To determine the DST status, compare the current offset with the standard offset:

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();
};
登入後複製

By calling isDstObserved() on a date object, you can verify if DST is active.

Example:

var today = new Date();
if (today.isDstObserved()) {
    console.log("Daylight saving time is in effect!");
}
登入後複製

以上是如何確定夏令時 (DST) 是否生效?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板