我想为特定时区创建一个 javascript 时钟
我想向我的网站添加一个时钟,显示中部时区的时间。我已经包含了迄今为止的代码。我想用它来告诉用户我网站上的其他工具何时将处于活动状态(它们在中部时区的夜间不活动)。有谁知道如何(1)将其锁定到中央时间; (2) 也许从晚上 8:00 到早上 7:30 将其变成红色(表示工具已关闭)?
<script type="text/javascript">
function GetClock(){
d = new Date();
nhour = d.getHours();
nmin = d.getMinutes();
if(nhour == 0) {ap = " AM";nhour = 12;}
else if(nhour <= 11) {ap = " AM";}
else if(nhour == 12) {ap = " PM";}
else if(nhour >= 13) {ap = " PM";nhour -= 12;}
if(nmin <= 9) {nmin = "0" +nmin;}
document.getElementById('clockbox').innerHTML=""+nhour+":"+nmin+ap+"";
setTimeout("GetClock()", 1000);
}
window.onload=GetClock;
</script>
<div id="clockbox"></div>
I'd like to add a clock to my web site, displaying the time in the Central time zone. I have included the code I have thus far. I want to use this to tell users when other tools on my web site will be active (they are not active during the night in the Central time zone). Does anyone know how to (1) lock this to central time; and (2) perhaps turn it a red color from 8:00 p.m. - 7:30 a.m. (indicating the tools are turned off)?
<script type="text/javascript">
function GetClock(){
d = new Date();
nhour = d.getHours();
nmin = d.getMinutes();
if(nhour == 0) {ap = " AM";nhour = 12;}
else if(nhour <= 11) {ap = " AM";}
else if(nhour == 12) {ap = " PM";}
else if(nhour >= 13) {ap = " PM";nhour -= 12;}
if(nmin <= 9) {nmin = "0" +nmin;}
document.getElementById('clockbox').innerHTML=""+nhour+":"+nmin+ap+"";
setTimeout("GetClock()", 1000);
}
window.onload=GetClock;
</script>
<div id="clockbox"></div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Date 对象可以修改为您喜欢的任何值,并且会自动更正。
“风时钟(2);”将时区偏移设置为 +2 UTC。
带 am/pm 的版本
https://jsfiddle.net/dzyubak/rae8j6xn/
The Date object can be modified to any value you like and will be corrected automatically.
"windTheClock(2);" sets time zone offset to +2 UTC.
version w/ am/pm
https://jsfiddle.net/dzyubak/rae8j6xn/
如果您创建本地日期对象,它将位于本地系统的时区中(无论设置为何,可能不是实际的本地时区)。日期对象有一个 getTimezoneOffset 方法,该方法返回一个以分钟为单位的数字,如果将其添加到本地日期对象,则会将其设置为 UTC(本质上是 GMT)。然后,您可以减去“中央时间”(无论是什么)的偏移量以获得该时区的时间。
然后,您可以使用该日期对象来表示该区域中的时间。
如果时区是美国中部时区,则标准偏移量为-6小时,夏令时偏移量为-5小时。返回具有特定偏移量的日期对象的函数是:
给它适当的偏移量,它将返回具有该偏移量的日期对象。要获取美国标准中部时间:
对于美国中部夏令时:
要在特定时间之间执行某些操作,您可以执行以下操作:
If you create a local date object, it will be in the timezone of the local system (whatever that might be set to, which might not be the actual local time zone). Date objects have a getTimezoneOffset method that returns a number in minutes that, if added to the local date object, sets it to UTC (essentially GMT). You can then subtract the offset for "central time" (whatever that might be) to get a time in that timezone.
You can then use that date object for times in that zone.
If the timezone is the US Central time zone, the standard offset is -6 hours, the daylight saving offset is -5 hours. A function that returns a date object with a specific offset is:
Give it the appropriate offset and it will return a date object with that offset. To get US standard central time:
For US central daylight saving time:
To do something between specific times, you can do something like: