【JS】获取实时时间——结合DOM
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>d24c04429</title>
</head>
<body>
<button onclick="myTimer()">获取当前时间</button>
<div id="timeDisplay"></div>
<script>
function getDate() {
var today = new Date();
var dayOfWeek = ["日", "一", "二", "三", "四", "五", "六"][today.getDay()];
var dayOfMonth = today.getDate();
var month = today.getMonth() + 1;
var year = today.getFullYear();
var hours = today.getHours();
var minutes = today.getMinutes();
var seconds = today.getSeconds();
hours = (hours < 10 ? "0" : "") + hours;
minutes = (minutes < 10 ? "0" : "") + minutes;
seconds = (seconds < 10 ? "0" : "") + seconds;
document.getElementById("timeDisplay").innerHTML =
"今天是" + year + "年" + month + "月" + dayOfMonth + "日 星期" +
dayOfWeek + " " + hours + ":" + minutes + ":" + seconds;
}
function myTimer() {
document.querySelector("button").style.display = "none";
setInterval(getDate, 50);
}
</script>
</body>
</html>
打赏

微信扫一扫,打赏作者吧~