例子如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<span id="sp"></span>
<script>
function show(){
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth()+1;
month=month<10?"0"+month:month;
var day = date.getDate();
day=day<10?"0"+day:day;
var week = date.getDay();
week ="星期"+"日一二三四五六七".charAt(week);
var hours = date.getHours();
hours=hours<10?"0"+hours:hours;
var minute = date.getMinutes();
minute=minute<10?"0"+minute:minute;
var second = date.getSeconds();
second=second<10?"0"+second:second;
var result = year+"-"+month+"-"+day+" "+week+" "+hours+":"+minute+":"+second;
document.getElementById("sp").innerHTML=result;
console.log(result);
}
show();
setInterval("show()",1000);
</script>
</body>
</html>
效果: