JS生成日历

版权声明:本文为博主前端基础文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">

<title>Document</title>
<style>
#box {
font-size: 50px;
color: red;
}
</style>
</head>

<body>
<div id="box"></div>
<script>
function getToday() {
var week = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
var box = document.getElementById("box");
var today = new Date();

var yy = today.getFullYear(); //年

var mm = todouble(today.getMonth() + 1); //月,从0开始,0代表1月份

var dd = todouble(today.getDate());//日

var hh = todouble(today.getHours());//时

var MM = todouble(today.getMinutes());//分

var ss = todouble(today.getSeconds());//秒

var ee = today.getDay();//星期。从0开始,0代表周日

var dddd = yy + "年" + mm + "月" + dd + "日" + "&nbsp;&nbsp;" + hh + ":" + MM + ":" + ss + "&nbsp;&nbsp;" + week[ee];

box.innerHTML = dddd;
}

getToday();
var timer = setInterval(getToday, 1000);

function todouble(n) {
return n < 10 ? "0" + n : "" + n;
}

</script>
</body>

</html>

在这里插入图片描述