gpt4 book ai didi

javascript - JS时刻时区

转载 作者:行者123 更新时间:2023-11-28 19:15:40 27 4
gpt4 key购买 nike

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

关于javascript - JS时刻时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29908184/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com