Home > Web Front-end > JS Tutorial > body text

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

Mary-Kate Olsen
Release: 2024-11-15 02:10:02
Original
1014 people have browsed it

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();
};
Copy after login

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

The above is the detailed content of How Can I Determine if Daylight Saving Time (DST) is in Effect?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template