Check If DST (Daylight Saving Time) Is In Effect In JavaScript

Check If DST (Daylight Saving Time) Is In Effect In JavaScript

To check if DST (Daylight Saving Time) is in effect in JavaScript, you can use the getTimezoneOffset() method. Specific steps will be introduced in this article. Let’s follow together.

How to check if DST (Daylight Saving Time) is in effect in JavaScript

Daylight Saving Time is always 1 hour (60 minutes) earlier than usual.

To check if DST (Daylight Saving Time) is in effect. First, we check if the system’s time zone uses DST, then we need to check if the offset of the specified date is equal to the offset of the summer day. If this is true, it means that the DST is in effect.

Check if the system’s time zone uses DST

The getTimezoneOffset() method returns a number, which is the difference between the UTC zone and the local time zone of the same date (In minutes).

Syntax:

getTimezoneOffset()

If no appropriate data is available, the return value will be 0.

Example:

const date = new Date('2022-10-15');
const offset = date.getTimezoneOffset();
console.log(offset);

Output:

240

Since no country uses seven months of DST, the offset from UTC in January will differ from the region in July.

We created two dates: Jan 1 and Jul 1. Then use the getTimezoneOffset() method to get the offset of the two dates. Finally, compare the received offsets.

Example:

const date = new Date();

// Get the offset of Jan 1
const jan1 = new Date(date.getFullYear(), 0, 1).getTimezoneOffset();

// Get the offset of Jul 1
const jul1 = new Date(date.getFullYear(), 6, 1).getTimezoneOffset();
  
const result = jan1 !== jul1; // True if not equal
console.log(result);

Output:

true

We use the time zone (UTC-05:00) Eastern Time (USA and Canada) and this time zone uses DST.

Complete code

Standard time always has an offset greater than Daylight Saving Time. Therefore, between the two dates, Jan 1 and Jul 1, the date with the greater offset will be the date with standard time.

We then check if the difference at the specified date equals the difference at that standard date and time. If so, the date specified is not in DST.

Example:

function isDST(dstDate) {
    // Get the offset of Jan 1
    const jan1 = new Date(dstDate.getFullYear(), 0, 1).getTimezoneOffset();

    // Get the offset of Jul 1
    const jul1 = new Date(dstDate.getFullYear(), 6, 1).getTimezoneOffset();

    if (jan1 !== jul1) {
      	return Math.max(jan1, jul1) !== dstDate.getTimezoneOffset(); // True if not equal
    } else {
      	return jan1 !== jul1;
    }
}

console.log(isDST(new Date('2022-12-15')));
console.log(isDST(new Date()));

Output:

false
true

First, we check if the system’s time zone uses DST. If so, we will use the Math.max() function to get the larger offset.

The Math.max() function returns the largest of the numbers given as input parameters.

Syntax:

Math.max(value0, value1, ..., valueN)

Parameters:

  • value0, value1, …, valueN: Numbers where the largest value will be selected and returned.

Finally, compare the selected offset with the specified date offset.

Summary

This article has shown how to check if DST (Daylight Saving Time) is in effect in JavaScript. I hope the information in this article will be helpful to you. If you have any problems, please comment below. I will answer. Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *