时钟在不同的时区 [英] Clock in different time zones

查看:115
本文介绍了时钟在不同的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在网站上创建两个时钟,上面说了两次。一个来自伦敦,另一个来自纽约。



我已经能够创建一个时钟来读取计算机上的当前时间,但我不知道如何放置这是一个时区。



我到目前为止的代码是:

 < script type =text / javascriptlanguage =javascript> 

函数renderTime(){
var currentTime = new Date();
var diem =AM;
var h = currentTime.getHours();
var m = currentTime.getMinutes();
var s = currentTime.getSeconds();

if(h == 0){
h = 12
}否则如果(h> 12){
h = h - 12;
diem =PM;
}

if(h< 10){
h =0+ h;
}

if(m< 10){
m =0+ m;
}

if(s< 10){
s =0+ s;
}

var myClock = document.getElementById(clockDisplay);
myClock.textContent = h +:+ m +:+ s ++ diem;

setTimeout('renderTime()',1000);
}
renderTime();
< / script>

这适用于我创建的CSS样式,因此我可以使用特定字体的时钟。

解决方案

您可以使用



<$来增加或减去约会时间。

  currentTime.setHours(currentTime.getHours()+偏移量); 

其中抵消是任何小时数。



<我用该行和函数更新了jsfiddle,以便它接受偏移量的参数。这不是UTC偏移量,只是从系统时间添加多少小时。您可以通过获取当前UTC偏移量currentTime.getTimezoneOffset()/ 60



演示


I am trying to create two clocks on a website that says two times on it. One from London and the other from New York.

I have been able to create a clock that reads the current time on my computer but i'm not sure how to place a time zone into this.

The code I have so far is:

<script type="text/javascript" language="javascript">

function renderTime() {
    var currentTime = new Date();
    var diem = "AM";
    var h = currentTime.getHours();
    var m = currentTime.getMinutes();
    var s = currentTime.getSeconds();

    if (h == 0) {
        h = 12
    } else if (h > 12) {
        h = h - 12;
        diem = "PM";
    }

    if (h < 10) {
        h = "0" + h;
    }

    if (m < 10) {
        m = "0" + m;
    }

    if (s < 10) {
        s = "0" + s;
    }

    var myClock = document.getElementById ("clockDisplay");
    myClock.textContent = h + ":" + m + ":" + s + " " + diem;

    setTimeout ('renderTime()', 1000);
}
renderTime();
</script>

This is being applied to a CSS style I have created so I can have the clock in a specific typeface.

解决方案

You can add or subtract hours from your date with

currentTime.setHours(currentTime.getHours()+offset);

where offset is any number of hours.

I updated the jsfiddle with that line and the function so that it accepts a parameter for the offset. This is not the UTC offset, just how many hours to add from the system time. You can get the current UTC offset by currentTime.getTimezoneOffset()/60

Demo

这篇关于时钟在不同的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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