5

There are many solutions available in javascript to check whether the date is in daylight or not [ How to check if the DST (Daylight Saving Time) is in effect and if it is what's the offset? ].

But that solutions work only when someone is in the same time zone where daylight effect is applicable, ex. Suppose my current browser (client) is in IST (Indian standard - where there is no daylight time change) and I want to check whether the date is in daylight or not for ET (Eastern time - where daylight is -5 and standard is -4). Above solution (from link) would give me correct result only if my systems time zone is in ET not IST.

How can I calculate it?

Community
  • 1
  • 1
Akshay
  • 3,558
  • 4
  • 43
  • 77

2 Answers2

5

If you use moment.js and it's companion timezome script, they make checking for DST pretty easy:

Check out their docs for isDST()

Just for grins, here's a fiddle testing each timezone

if( moment.tz("America/New_York").isDST() ){
    $('#test').append('NY is currently in DST');
}
else{
    $('#test').append('NY is NOT currently in DST');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.3/moment.min.js"></script>
<script src="http://momentjs.com/downloads/moment-timezone-with-data.min.js"></script>



<div id="test"></div>
Wesley Smith
  • 19,401
  • 22
  • 85
  • 133
  • This throws an error: "TypeError: undefined is not an object (evaluating 'g.apply')" in moment-timezone-with-data.min.js. :-( – RobG Mar 24 '21 at 12:37
2

You can use luxon library to check is DST is active or not in particular area. all you need is timezone string and you can check is DST is active or not.

so first import DateTIme from luxon

import { DateTime } from "luxon";

then check if DST is active or not in the timezone

if (DateTime.local().setZone(timezone).isInDST) {
  console.log("Dst is active in ", timezone)
}
else {
  console.log("Dst is NOT active in ", timezone)
}

where timezone is the timezone you are looking for.