javascript 不同时区的时钟

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10883441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:20:46  来源:igfitidea点击:

Clock in different time zones

javascriptjquerytimezoneclock

提问by Houmy

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.

这将应用于我创建的 CSS 样式,以便我可以使用特定字体的时钟。

采纳答案by sachleen

You can add or subtract hours from your date with

您可以在日期中添加或减去小时数

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

where offset is any number of hours.

其中 offset 是任意小时数。

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

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

Demo

演示

回答by manuskc

currentTime.getTimezoneOffset()will give you the time difference between Greenwich Mean Time (GMT) and local time, in minutes.

currentTime.getTimezoneOffset()将为您提供格林威治标准时间 (GMT) 和本地时间之间的时差,以分钟为单位。

You can use the value to calculate time in required timezone.

您可以使用该值计算所需时区的时间。

回答by Matt Johnson-Pint

To do this properly, you will need a time zone database, such as one of the ones I listed here.

正确执行此操作,您将需要一个时区数据库,例如我在此处列出的数据库之一。

All of the other answers to this question are making the mistake in thinking that a "time zone" and a "time zone offset" are the same thing. They are not. For example, the time zone for London is Europe/London, which can have either a +0 or +1 offset depending on what time of year it is. The time zone for New York is America/New_York, which can have either a -5 or -4 offset based on a completely different set of dates than London.

这个问题的所有其他答案都错误地认为“时区”和“时区偏移”是同一回事。他们不是。例如,伦敦的时区是Europe/London,它可以有 +0 或 +1 偏移量,具体取决于它是一年中的什么时间。纽约的时区是America/New_York,根据与伦敦完全不同的日期集,它可以有 -5 或 -4 的偏移量。

You might want to look at moment-timezone:

您可能想查看moment-timezone

moment().tz("America/New_York").format()

回答by RGB

Check out http://www.datejs.com/it handles dates and timezones nicely

查看http://www.datejs.com/它可以很好地处理日期和时区

if you check out the demo, in the text box, put 1 GMTvs 1 MSTor 1 ESTand it will pop out a nice date for you wrt/ that time zone

如果您查看演示,请在文本框中输入1 GMTvs1 MST1 EST,它会为您弹出一个适合您/那个时区的日期

回答by Houmy

Thank you everyone for your help. It was all getting a bit confusing but I found a good example here which is how I ended up working it out. Check out example 4: http://www.ajaxupdates.com/jclock-jquery-clock-plugin/

感谢大家的帮助。这一切都变得有点令人困惑,但我在这里找到了一个很好的例子,这就是我最终解决它的方式。查看示例 4:http: //www.ajaxupdates.com/jclock-jquery-clock-plugin/

Hope it's useful for someone else!

希望对其他人有用!

回答by Jara Lowell

I use on m php website the following:

我在 m php 网站上使用以下内容:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"
<script type="text/javascript">
$(function() {
    getStatus();
});

function getStatus() {
<?php
echo "    var z = '".strftime("%Z",strtotime("now"))."';\n";
?>
    var d = new Date();
    var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
    var offset = -8;
    if(z == "PDT") offset = -7;
    var currentTime = new Date(utc + (3600000*offset));
    var currentHours = currentTime.getHours ( );
    var currentMinutes = currentTime.getMinutes ( );
    currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
    var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";
    currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;
    currentHours = ( currentHours == 0 ) ? 12 : currentHours;
    var currentTimeString = currentHours + ":" + currentMinutes + ": " + timeOfDay + " " + z;

    $('td#clock').html(currentTimeString);
    setTimeout("getStatus()",5000);
}
</script>

Where i use a table, so this will fill

我在哪里使用桌子,所以这会填满

<table><tr><td id='clock'></td></tr></table>

with the time from Los Angeles (PDT or PST) however my server also runs on that time so passing the strftime from your server might not produce the same effect. But as stated this was my resolve to get a working time in a other zone.

与来自洛杉矶的时间(PDT 或 PST)但是我的服务器也在那个时间运行,因此从您的服务器传递 strftime 可能不会产生相同的效果。但如前所述,这是我决心在另一个区域工作的决心。