Javascript - check if daytime is valid for different time zones

There are many solutions in javascript to check if a date is in daylight or not [ How to check if daylight saving time (daylight saving time) is valid, and if it is that offset? .

But these solutions work only when someone is in the same time zone where the daylight effect is applicable, ex. Suppose my current browser (client) is in IST (Indian Standard - where there is no daytime ), and I want to check if the date is in the daytime or not for ET (Eastern Time ), where daylight is - 5, and the standard is -4). The above solution (from the link) will give me the correct result only if my system time zone is in ET not IST.

How can i calculate it?

+5
source share
1 answer

If you use moment.js and its companion timezome script, they check DST pretty easily:

Check docs for isDST ()

Just for fun, here's a fiddle checking every time zone

 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> 
+5
source

Source: https://habr.com/ru/post/1210086/


All Articles