【问题标题】:javascript - check if daylight time is in effect for different timezonejavascript - 检查不同时区的夏令时是否有效
【发布时间】:2015-02-26 07:55:20
【问题描述】:

在 javascript 中有许多可用的解决方案来检查日期是否在白天 [How to check if the DST (Daylight Saving Time) is in effect and if it is what's the offset?]。

但该解决方案仅在某人处于适用日光效果的同一时区时才有效, 前任。假设我的 当前浏览器(客户端)在 IST 中(印度标准 - 没有日光时间 更改)并且我想检查日期是否在日光下ET(东部时间 - 日光为 -5,标准为 -4)。仅当我的系统时区位于 ET 而不是 IST 时,上述解决方案(来自链接)才会给我正确的结果。

如何计算?

【问题讨论】:

    标签: javascript timezone dst timezone-offset


    【解决方案1】:

    如果您使用 moment.js 和它的配套 timezome 脚本,它们可以很容易地检查 DST:

    查看他们的docs for isDST()

    只是为了笑,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>

    【讨论】:

    • 这会在 moment-timezone-with-data.min.js 中引发错误:“TypeError: undefined is not an object (evalating 'g.apply')”。 :-(
    猜你喜欢
    • 2018-01-12
    • 2012-05-26
    • 2013-12-23
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    • 2017-03-17
    • 2016-07-29
    • 2017-10-22
    相关资源
    最近更新 更多