JS实现动态日期
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1 id="time"></h1>
<script type="text/javascript">
function week(option){
// switch(option){
// case 1:
// return "星期一";
// case 2:
// return "星期二";
// case 3:
// return "星期三";
// case 4:
// return "星期四";
// case 5:
// return "星期五";
// case 6:
// return "星期六";
// case 0:
// return "星期日";
// }
var week = "日一二三四五六";
return "星期" + week.charAt(option);
}
function setTime(){
var dateTime = new Date();
var year = dateTime.getFullYear();
var month = dateTime.getMonth()+1;
month = month < 10 ? "0" + month : month;
var date = dateTime.getDate();
date = date < 10 ? "0" + date : date;
var hour = dateTime.getHours();
hour = hour < 10 ? "0" + hour : hour;
var minute = dateTime.getMinutes();
minute = minute < 10 ? "0" + minute : minute;
var second = dateTime.getSeconds();
second = second < 10 ? "0" + second : second;
var day = week(dateTime.getDay());
var str = `${year}年${month}月${date}日 ${hour}:${minute}:${second} ${day}`;
// console.log(str);
document.getElementById("time").innerHTML = str;
// setTimeout("setTime()",1000);
}
setTime();
setInterval("setTime()",1000);
function test(message){
console.log(message);
}
</script>
</body>
</html>
JS实现动态日期