UTC时间转换
我想在我的页面上显示一定的时间,而不是访问者的本地时间,而是我目前所在的时区。 我使用我在Plus2net上找到的代码:
function display_ct7() {
var x = new Date()
var ampm = x.getHours( ) >= 12 ? ' pm' : ' am';
hours = x.getHours( ) % 12;
hours = hours ? hours : 12;
hours=hours.toString().length==1? 0+hours.toString() : hours;
var minutes=x.getMinutes().toString()
minutes=minutes.length==1 ? 0+minutes : minutes;
var seconds=x.getSeconds().toString()
seconds=seconds.length==1 ? 0+seconds : seconds;
var x1=hours + ":" + minutes + ":" + seconds + " " + ampm;
document.getElementById('ct7').innerHTML = x1;
display_c7();
}
function display_c7(){
var refresh=1000; // Refresh rate in milli seconds
mytime=setTimeout('display_ct7()',refresh)
}
display_c7()
现在显示在德国(UTC +2H)的时间。 Dreamweaver说,它有效,但在代码中有一些错误。由于我不习惯JS,有人可以帮助我“清洁”此代码?先感谢您...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用 为此目的。您可以通过任何
iana timezone
显示在此区域。对于德国来说,这将是“欧洲/柏林”。
在以下代码中,如果没有时区,我们将显示用户的本地时间。
您可以轻松地添加其中一个或多个时钟。
I would suggest using
Date.toLocaleTimeString()
for this purpose. You can pass anyIANA timezone
you wish and the output will be displayed in this zone.For Germany, this will be 'Europe/Berlin'.
In the below code, we'll display the user's local time if no timezone is passed.
You can add one or more of these clocks easily.