【发布时间】:2016-11-12 09:35:21
【问题描述】:
我有 12 小时格式时间的简单代码
// This function gets the current time and injects it into the DOM
function updateClock() {
// Gets the current time
var now = new Date();
// Get the hours, minutes and seconds from the current time
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
// Format hours, minutes and seconds
if (hours > 12) {
hours = hours - 12;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
// Gets the element we want to inject the clock into
var elem = document.getElementById('clock');
// Sets the elements inner HTML value to our clock data
elem.innerHTML = hours + ':' + minutes + ':' + seconds ;
}
我想添加上午/下午请帮助我提前谢谢 我只是 javascript 的初学者
【问题讨论】:
-
这里有一个提示:如果你必须减去 12 小时,那就是下午。否则,就是 AM。
标签: javascript