1

我试图在我的网站上获得三个不同的动态时区时钟。我在此站点上找到了以下 js 代码(保存为 myClocks.js 并包含在我的 html 站点的标题中):

var clock1 = new Date();
var clock2 = new Date();
var clock3 = new Date();
clock2.setHours(clock2.getHours() + 3);
clock3.setHours(clock3.getHours() - 5);
clock1.getUTCHours();
clock1.getUTCMinutes();
clock1.getUTCSeconds();
clock2.getUTCHours();
clock2.getUTCMinutes();
clock2.getUTCSeconds();
clock3.getUTCHours();
clock3.getUTCMinutes();
clock3.getUTCSeconds();

如何编码“显示”以在我的 HTML 页面上任何我想要的地方显示它?例如,作为一个名为 的 id clocks,看起来如下所示:

纽约:02:12:02 巴黎:17:01:24 莫斯科:22:23:42

提前谢谢了。

4

3 回答 3

1
<html><head></head><body>
<script language="javascript">
ourDate = new Date();
document.write("The time and date at your computer's location is: "
+ ourDate.toLocaleString() 
+ ".<br/>");
document.write("The time zone offset between local time and GMT is " 
+ ourDate.getTimezoneOffset() 
+ " minutes.<br/>");
document.write("The time and date (GMT) is: " 
+ ourDate.toGMTString() 
+ ".<br/>");
</script>
</body></html>
于 2013-07-20T10:14:44.683 回答
0

innerHTML是你需要的。尝试类似:

window.onload = function(){ // It is important to wait till DOM is ready!
   var clocks_str = clock3.getUTCHours()+" "+ clock3.getUTCMinutes()+" "+clock3.getUTCSeconds();
   document.getElementById("clocks").innerHTML = clocks_str ;
}

如果你想要它是动态的,请使用setIntervalmethod ,如下所示:

 var clocks_interval;
 var clocks_box;

 window.onload = startClocks;

 function startClocks(){
    clocks_box = document.getElementById("clocks");
    clocks_interval = setInterval(updateClocks , 1000); // 1000 means 1 second
 }

 function updateClocks (){
    var clocks_str = clock3.getUTCHours()+" "+ clock3.getUTCMinutes()+" "+clock3.getUTCSeconds();
    clocks_box.innerHTML = clocks_str ;
 }
于 2013-07-20T10:08:28.217 回答
0

您可以创建一个 div 或其他 HTML 并使用“innerHTML”。

document.getElementById("clocks").innerHTML = clock1.getUTCHours();
于 2013-07-20T10:09:45.630 回答