-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar_generator.html
135 lines (115 loc) · 4.48 KB
/
calendar_generator.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>全年月曆顯示</title>
<style>
.calendar-container {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.month-container {
width: 30%;
border: 1px solid #ddd;
padding: 10px;
box-sizing: border-box;
}
.month-container h2 {
text-align: center;
margin: 0;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 10px;
}
th, td {
border: 1px solid #ddd;
text-align: center;
padding: 5px;
}
th {
background-color: #f2f2f2;
}
.weekend {
background-color: #cceeff;
}
</style>
</head>
<body>
<h1>全年月曆顯示</h1>
<label for="year">年份:</label>
<select id="year"></select>
<div id="calendar" class="calendar-container"></div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const yearSelect = document.getElementById('year');
const calendarDiv = document.getElementById('calendar');
// 填充年份選單
const currentYear = new Date().getFullYear();
for (let i = currentYear - 10; i <= currentYear + 10; i++) {
const option = document.createElement('option');
option.value = i;
option.textContent = i;
yearSelect.appendChild(option);
}
// 當年份更改時生成全年月曆
yearSelect.addEventListener('change', generateYearlyCalendar);
function generateYearlyCalendar() {
const year = parseInt(yearSelect.value);
if (!year) return;
// 清除先前的月曆
calendarDiv.innerHTML = '';
const months = [
"1月", "2月", "3月", "4月", "5月",
"6月", "7月", "8月", "9月", "10月",
"11月", "12月"
];
// 為每個月份生成月曆
months.forEach((monthName, month) => {
const monthContainer = document.createElement('div');
monthContainer.classList.add('month-container');
const monthTitle = document.createElement('h2');
monthTitle.textContent = `${year}年 ${monthName}`;
monthContainer.appendChild(monthTitle);
const table = document.createElement('table');
const headerRow = document.createElement('tr');
const daysOfWeek = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
daysOfWeek.forEach(day => {
const th = document.createElement('th');
th.textContent = day;
headerRow.appendChild(th);
});
table.appendChild(headerRow);
const firstDay = new Date(year, month, 1).getDay();
const daysInMonth = new Date(year, month + 1, 0).getDate();
let row = document.createElement('tr');
for (let i = 0; i < firstDay; i++) {
row.appendChild(document.createElement('td'));
}
for (let day = 1; day <= daysInMonth; day++) {
const cell = document.createElement('td');
cell.textContent = day;
if ((firstDay + day - 1) % 7 === 0 || (firstDay + day - 1) % 7 === 6) {
cell.classList.add('weekend');
}
row.appendChild(cell);
if ((firstDay + day) % 7 === 0) {
table.appendChild(row);
row = document.createElement('tr');
}
}
table.appendChild(row);
monthContainer.appendChild(table);
calendarDiv.appendChild(monthContainer);
});
}
// 預設為當前年份
yearSelect.value = currentYear;
generateYearlyCalendar();
});
</script>
</body>
</html>