admin管理员组文章数量:1289363
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?
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?
Share Improve this question edited May 23, 2017 at 12:19 CommunityBot 11 silver badge asked Dec 30, 2014 at 6:23 AkshayAkshay 3,8664 gold badges49 silver badges84 bronze badges3 Answers
Reset to default 5If you use moment.js
and it's panion 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./ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare./ajax/libs/moment.js/2.8.3/moment.min.js"></script>
<script src="http://momentjs./downloads/moment-timezone-with-data.min.js"></script>
<div id="test"></div>
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.
This is now possible without a library, thanks to the Intl.DateTimeFormat API, which is supported in all modern browsers and Node.js 13.0+. With that API, you can emit a string that includes the time zone, which differs based on daylight savings.
For example, in the Eastern Time Zone, the time zone string is EST
when it's not daylight savings and EDT
when it is. So to determine whether a given date
object is during daylight savings in that time zone, you could do this:
const tzFormatter = new Intl.DateTimeFormat("en-US", {timeZone: "America/New_York", timeZoneName: "short"});
const isDST = tzFormatter.format(date).endsWith("EDT");
本文标签: javascriptcheck if daylight time is in effect for different timezoneStack Overflow
版权声明:本文标题:javascript - check if daylight time is in effect for different timezone - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741415914a2377509.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论