Js片刻时区

时间:2015-04-28 00:20:55

标签: javascript momentjs

我有以下代码来操纵哥本哈根的时间。我想知道如何使用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()">

1 个答案:

答案 0 :(得分:3)

如果您需要特定的时区,则需要时区插件。否则你可以把它留下来。提供.tz()

&#13;
&#13;
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);
}
&#13;
<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>
&#13;
&#13;
&#13;