UTC时间转换

发布于 2025-02-12 23:34:27 字数 828 浏览 0 评论 0 原文

我想在我的页面上显示一定的时间,而不是访问者的本地时间,而是我目前所在的时区。 我使用我在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,有人可以帮助我“清洁”此代码?先感谢您...

I want to display a certain time on my page, NOT the local time of the visitor, but the timezone where I am at the moment.
I use this code I found on 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()

It shows now the time in Germany (UTC +2h).
And it works but it has some errors in the code, says dreamweaver. Due to the fact that I am not used to js, can someone maybe help me to "clean" this code? Thank you in advance...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥醉 2025-02-19 23:34:27

我建议使用 为此目的。您可以通过任何 iana timezone 显示在此区域。

对于德国来说,这将是“欧洲/柏林”。

在以下代码中,如果没有时区,我们将显示用户的本地时间。

您可以轻松地添加其中一个或多个时钟。

// If timeZone is not passed we'll return local time...
function getTime(timeZone) {
    let time = new Date().toLocaleTimeString('en-US', { timeZone });
    // Ensure lower case and leading zero..
    return time.toLowerCase().padStart(11, '0');
}

function displayClockInElement(elementId, timeZone) {
    document.getElementById(elementId).innerHTML = getTime(timeZone);
}
 
function startClock(refreshRate, elementId, timeZone) {
    displayClockInElement(elementId, timeZone);
    setInterval(displayClockInElement, refreshRate, elementId, timeZone)
}

startClock(1000, 'local');
startClock(1000, 'de', 'Europe/Berlin');
startClock(1000, 'utc', 'UTC');
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<table class="table table-sm table-striped table-bordered">
<thead>
  <tr><th>Timezone</th><th>Time</th></tr>
</thead>
<tbody>
  <tr><td>Local</td><td id='local'></td></tr>
  <tr><td>UTC</td><td id='utc'></td></tr>
  <tr><td>Berlin</td><td id='de'></td></tr>
</tbody>
</table>

I would suggest using Date.toLocaleTimeString() for this purpose. You can pass any IANA 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.

// If timeZone is not passed we'll return local time...
function getTime(timeZone) {
    let time = new Date().toLocaleTimeString('en-US', { timeZone });
    // Ensure lower case and leading zero..
    return time.toLowerCase().padStart(11, '0');
}

function displayClockInElement(elementId, timeZone) {
    document.getElementById(elementId).innerHTML = getTime(timeZone);
}
 
function startClock(refreshRate, elementId, timeZone) {
    displayClockInElement(elementId, timeZone);
    setInterval(displayClockInElement, refreshRate, elementId, timeZone)
}

startClock(1000, 'local');
startClock(1000, 'de', 'Europe/Berlin');
startClock(1000, 'utc', 'UTC');
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<table class="table table-sm table-striped table-bordered">
<thead>
  <tr><th>Timezone</th><th>Time</th></tr>
</thead>
<tbody>
  <tr><td>Local</td><td id='local'></td></tr>
  <tr><td>UTC</td><td id='utc'></td></tr>
  <tr><td>Berlin</td><td id='de'></td></tr>
</tbody>
</table>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文