Js 时刻时区



我有以下代码来操作哥本哈根的时间。我想知道如何使用 js 时刻实现这一点?

function startTime() {
    var today=new Date();
    var i=today.getHours();
    var h = i-2;
    var m=today.getMinutes();
    var s=today.getSeconds();
    m = checkTime(m);
    s = checkTime(s);
    document.getElementById('localtime').innerHTML = h+":"+m+":"+s;
    var t = setTimeout(function(){startTime()},500);
}
function checkTime(i) {
    if (i<10) {i = "0" + i};  // add zero in front of numbers < 10
    return i;
}
<body onload="startTime()">

如果你想要特定的时区,你需要时区插件。 否则,您可以将其排除在外。 提供 .tz()

function startTime() {
//local time
document.getElementById('localtime').innerHTML =
  moment().format('hh:mm:ss');
//copenhagen timezone
document.getElementById('copenhagen').innerHTML =
  moment.tz('Europe/Copenhagen').format('hh:mm:ss');
var t = setTimeout(function(){startTime()},500);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.2/moment-with-locales.js"></script>
<script src="http://momentjs.com/downloads/moment-timezone-with-data.js"></script>
<body onload="startTime()">
  <div id="localtime"></div>
  <div id="copenhagen"></div>
</body>

最新更新