<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>js date_displayAClock</title>
<script>
function displayAClock() {
/*功能:展示时间。*/
let today = new Date();
let h = today.getHours();
let m = today.getMinutes();
let s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
// document.getElementById('txt').innerHTML = h + ":" + m + ":" + s;
// txt,本地时间字符串,2018/10/14 下午12:15:48。
document.getElementById('txt').innerHTML = today.toLocaleString();
let timeout = setTimeout(displayAClock, 1000);
}
function checkTime(i) {
/*功能:在小于10的数字前加一个‘0’。*/
if (i < 10) {
i = "0" + i;
}
return i;
}
</script>
</head>
<body onload="displayAClock()">
<div id="txt"></div>
</body>
</html>