效果如下图所示(每一秒刷新一次):
详细代码如下:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript的时钟小程序</title>
<script type="text/javascript">
function showDate(){
//获取系统时间
var date = new Date();
//获取年月日时分秒
var str = date.getFullYear() + "年" + (date.getMonth()+1) + "月"
+date.getDate() +"日 " + date.getHours() + ":"
+date.getMinutes() + ":" +date.getSeconds();
document.getElementById("mydate").innerHTML =str; //将str赋值给span标签
}
setInterval("showDate()", 1000); //设置每1000毫秒刷新一次
</script>
</head>
<body οnlοad="showDate()">
<span id="mydate"></span>
</body>
</html>