我正在使用一个简单的 javascript 来显示我所在国家/地区的当地时间,但它似乎不正确并且没有以正确的顺序正确显示。午夜 12 点之后,时钟应该重新设置为上午,但它一直显示下午的时间。
身体:
<body onload="updateClock(); setInterval('updateClock()', 1000 )">
//div tags, elements etc
</body>
JS:
function updateClock (){
var currentTime = new Date();
var currentHours = currentTime.getUTCHours() + 8;
var currentMinutes = currentTime.getUTCMinutes();
var currentSeconds = currentTime.getUTCSeconds();
// var bucurestiOffset = 3*60000;
// var userOffset = currentTime.getTimezoneOffset()*60000;
currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";
currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;
currentHours = ( currentHours == 0 ) ? 12 : currentHours;
var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}
结果是这样的:13:57 PM,当它应该是1:57 AM 时。不知道如何解决。