首页 > 其他分享 >实现计时、时钟,实时获取当前年月日时分秒,记录时间(获取

实现计时、时钟,实时获取当前年月日时分秒,记录时间(获取

时间:2022-12-22 13:07:01浏览次数:35  
标签:seconds month hours 获取 let date 记录时间 时分秒 day


 

 

 

实现计时、时钟,实时获取当前年月日时分秒,记录时间(获取_html

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>现在时刻</title>
</head>
<body>
<div id="now"></div>
</body>
<script>
/*实现计时,记录时间(获取当前的时间,开始计时,计时过程相当于你的电脑时钟)0000年00月00日 00:00:00*/
function now(id) {
let div = document.getElementById(id);
let date = new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
let hours = date.getHours();
let minutes = date.getMinutes();
let seconds = date.getSeconds();
month = month < 10 ? "0" + month : month;
day = day < 10 ? "0" + day : day;
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
div.innerHTML = "现在时间:" + year + "年" + month + "月" + day + "日" + " " + hours + ":" + minutes + ":" + seconds;/* div.innerHTML = "现在时间:" + year + "年" + month + "月" + day + "日" + " " + hours + "时" + minutes + "分" + seconds + "秒";*/
}

now("now"), setInterval("now('now')", 1000);
</script>
</html>

标签:seconds,month,hours,获取,let,date,记录时间,时分秒,day
From: https://blog.51cto.com/u_15920212/5962645

相关文章