Js时刻时区
Js moments timezone
我有以下代码来操纵哥本哈根的时间。我想知道如何使用 js moment 来实现它?
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>
我有以下代码来操纵哥本哈根的时间。我想知道如何使用 js moment 来实现它?
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>