/
demo.html
100 lines (74 loc) · 2.86 KB
/
demo.html
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<!DOCTYPE html>
<html>
<head>
<meta charset=UTF-8>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- <script src="https://cdn.jsdelivr.net/gh/magiclen/ts-year-helper/dist/year-helper.min.js"></script> -->
<script src="./dist/year-helper.min.js"></script>
<title>year-helper</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3 p-lg-5">
<div id="demo-year-helper" class="container">
<div class="row g-2 justify-content-center">
<div class="col-auto">
<select id="year" class="form-select"></select>
</div>
<div class="col-auto">
<select id="month" class="form-select"></select>
</div>
<div class="col-auto">
<select id="date" class="form-select"></select>
</div>
</div>
</div>
<script>
const year = document.getElementById("year");
const month = document.getElementById("month");
const date = document.getElementById("date");
const now = new Date();
const currentYear = now.getFullYear();
for (let i = -10; i <= 10; i++) {
const y = currentYear + i;
const t = y.toString();
const option = document.createElement("option");
option.setAttribute("value", t);
option.innerHTML = t;
year.appendChild(option);
}
year.value = currentYear.toString();
for (let i = 1; i <= 12; i++) {
const t = i.toString();
const option = document.createElement("option");
option.setAttribute("value", t);
option.innerHTML = t;
month.appendChild(option);
}
month.value = `${now.getMonth() + 1}`;
for (let i = 1; i <= 31; i++) {
const t = i.toString();
const option = document.createElement("option");
option.setAttribute("value", t);
option.innerHTML = t;
date.appendChild(option);
}
date.value = `${now.getDate()}`;
const yearOrMonthChanged = () => {
const days = YearHelper.getDaysInMonth(parseInt(year.value), parseInt(month.value));
console.log(`${year.value}-${month.value} has ${days} days`);
for (let i = 28; i < days; i++) {
date.children[i].disabled = false;
}
for (let i = days; i < 31; i++) {
date.children[i].disabled = true;
}
if (date.selectedIndex >= days) {
date.selectedIndex = 0;
}
};
year.addEventListener("change", yearOrMonthChanged);
month.addEventListener("change", yearOrMonthChanged);
yearOrMonthChanged();
</script>
</body>
</html>