sf.html
html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>时间</title>
<script>
function showTime()
{
var time=new Date();
var date=time.getDate();
var year=time.getFullYear();
var month=time.getMonth()+1;
var hour=time.getHours();
var minute=time.getMinutes();
var second=time.getSeconds();
var nianyueri=year+"年"+" "+month+"月"+" "+date+"日";
var mytime=hour+":"+minute+":"+second;
document.getElementById("nianyueri").innerHTML=nianyueri;
document.getElementById("sj").innerHTML=mytime;
setTimeout("showTime()",1000);
}
</script>
</head>
<body>
<div>
<div id="nianyueri"></div>
<div id="sj">20:57</div>
<script>
showTime();
</script>
</div>
</body>
</html>
运行后
单独显示年份
year.html
html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>年</title>
<script>
function showTime()
{
var time=new Date();
var year=time.getFullYear();
var showyear=year+'年';
document.getElementById("Year").innerHTML=showyear;
}
</script>
</head>
<body>
<div id="Year"></div>
<script>showTime()</script>
</body>
</html>