描述:
使用JavaScript,实现一个实时时钟。自动执行。实时更新时间。
效果展示:
代码演示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>时钟小程序</title>
<style>
span{
font-size: 30px;
}
</style>
</head>
<body>
<h1>时钟:<span id="dt"></span></h1>
<h1>今天是:
<span id="year"></span>年
<span id="month"></span>月
<span id="day"></span>号
<span id="hour"></span>点
<span id="minu"></span>分
<span id="second"></span>秒,
星期:<span id="week"></span>
</h1>
<script>
function time(){
var sj = new Date();
var dt = document.getElementById("dt");
dt.innerHTML = sj.toLocaleDateString()+","+sj.toLocaleTimeString();
var year = document.getElementById("year");
var month = document.getElementById("month");
var day = document.getElementById("day");
var hour = document.getElementById("hour");
var minu = document.getElementById("minu");
var second = document.getElementById("second");
var week = document.getElementById("week");
year.innerHTML = sj.getFullYear();
month.innerHTML = sj.getMonth();
day.innerHTML = sj.getDate();
hour.innerHTML = sj.getHours();
minu.innerHTML = sj.getMinutes();
second.innerHTML = sj.getSeconds();
week.innerHTML = sj.getDay();
}
var vt = setInterval(time,1000);
</script>
</body>
</html>