使用JavaScript在一个特定时区内抵消DST的时间 [英] Offset time for DST in one specific timezone using JavaScript

查看:86
本文介绍了使用JavaScript在一个特定时区内抵消DST的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果目前在太平洋时区的DST,我需要将时间缩短一小时。如何确定太平洋时区的当前夏令时状态,无论用户的当地时区如何?

I need to offset the time by an hour if it's currently DST in the Pacific Time Zone. How can I determine the current daylight savings status of the Pacific Time Zone, regardless of the user's local timezone?

这是我到目前为止所拥有的。第4行中的dst只是一个函数的占位符,可以告诉我该区域的夏令时是否有效。

Here's what I have so far. "dst" in line 4 is just a placeholder for a function that would tell me if daylight savings time is active in that zone.

function checkTime() {  
    var d = new Date();  
    var hour = d.getUTCHours();  
    var offset = dst ? 7 : 8; // is pacific time currently in daylight savings?

    // is it currently 6 AM, 2 PM, or 10 PM?
    if (hour === ((6 + offset) % 24) || hour === ((14 + offset) % 24) || hour === ((22 + offset) % 24)) {  
      // do stuff
    }
}

checkTime();


推荐答案

唯一可行的方法确定您是否拥有可以询问的服务器上可用的DST信息(包括任何更新)。用户的PC甚至不需要安装时区信息。

The only real way you will be able to tell for sure is if you have the DST information (including any updates) available on a server you can ask. The user's PC does not need to even have the timezone information installed.

然后你需要一个动态脚本来获得DST状态。

Then you need a "dynamic" script to get the DST status.

可能的方法:
引用一个PHP / ASPX / JSP / CFM / ASP页面,它生成一个javascript来设置一个变量,指示DST是否处于活动状态,或当前的分钟数偏移。

Possible methods: Reference a PHP/ASPX/JSP/CFM/ASP page that generates a javascript to set a variable that indicates if DST is active or not, or the current number of minutes offset.

直接在您的(希望动态)页面中编写脚本。

Write the script in your (hopefully dynamic) page directly.

使用AJAX / REST / JSON在某个地方访问日期/时间服务,可以告诉您DST状态或时区/位置偏离UTC的分钟数。

Use AJAX/REST/JSON to access a Date/Time service somewhere that can tell you the DST status or number of minutes offset from UTC for a timezone/location.

由于DST,分钟优于小时世界各地的抵消实际上只有几分钟,而不是几小时。

Minutes is preferable to hours because DST offsets are actually in minutes around the world, not hours.

这篇关于使用JavaScript在一个特定时区内抵消DST的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆