[javascript]代码库
<div id="clock"></div>
<script>
function changeClock() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
var day = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
// 补零
if (month < 10) {
month = "0" + month;
}
if (day < 10) {
day = "0" + day;
}
if (hour < 10) {
hour = "0" + hour;
}
if (minute < 10) {
minute = "0" + minute;
}
if (second < 10) {
second = "0" + second;
}
// 显示时钟
document.getElementById("clock").innerHTML = year + "-" + month + "-"
+ day + " " + hour + ":" + minute + ":" + second;
}
window.setInterval(changeClock, 1000);
</script>
[代码运行效果截图]