<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="show-time"></div>
<div id="show-week"></div>
<div id="show-week-cn"></div>
<div id="show-date"></div>
</body>
</html>
<script type="text/javascript">
getTime()
setInterval("getTime();", 1000); //每隔一秒执行一次
//取得系统当前时间
function getTime() {
let myDate = new Date();
let date = myDate.toLocaleDateString();
let hours = myDate.getHours();
let minutes = myDate.getMinutes();
let seconds = myDate.getSeconds();
if (hours < 10) {
hours = "0" + hours;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
let str = hours + ":" + minutes + ":" + seconds;
document.getElementById("show-time").innerHTML = str; //将值赋给div
}
// 当前周几
getWeek();
function getWeek() {
let str = "";
let d = new Date();
switch (d.getDay()) {
case 0:
str = str + "Sun.";
break;
case 1:
str = str + "Mon.";
break;
case 2:
str = str + "Tue.";
break;
case 3:
str = str + "Wed.";
break;
case 4:
str = str + "Thu.";
break;
case 5:
str = str + "Fri.";
break;
case 6:
str = str + "Sat.";
break;
}
document.getElementById("show-week").innerHTML = str; //将值赋给div
}
// 当前周几 中文
getWeekCn();
function getWeekCn() {
let str = "";
let d = new Date();
switch (d.getDay()) {
case 0:
str = str + "星期天";
break;
case 1:
str = str + "星期一";
break;
case 2:
str = str + "星期二";
break;
case 3:
str = str + "星期三";
break;
case 4:
str = str + "星期四";
break;
case 5:
str = str + "星期五";
break;
case 6:
str = str + "星期五";
break;
}
document.getElementById("show-week-cn").innerHTML = str; //将值赋给div
}
// 获取当前日期
getNowFormatDate();
function getNowFormatDate() {
let date = new Date();
let seperator1 = "-";
let seperator2 = "-";
let seperator3 = "";
let year = date.getFullYear();
let month = date.getMonth() + 1;
let strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
let str = year + seperator1 + month + seperator2 + strDate + seperator3;
document.getElementById("show-date").innerHTML = str; //将值赋给div
}
</script>
实现效果,复制成html直接浏览器打开