<label for="year">选择年份:</label>
<select id="year" onchange="updateWeekDates()">
<option value="2022">2022</option>
<option value="2023">2023</option>
<!-- 添加更多年份选项 -->
</select>
<label for="week">选择日期:</label>
<select id="week">
<!-- 这里的选项将会通过 JavaScript 动态生成 -->
</select>
function updateWeekDates() {
var yearSelect = document.getElementById("year");
var weekSelect = document.getElementById("week");
var selectedYear = yearSelect.value;
// 清空日期下拉框的选项
weekSelect.innerHTML = "";
// 根据选择的年份生成日期选项
var startDate = new Date(selectedYear, 0, 1); // 选择年份的第一天
var endDate = new Date(selectedYear, 11, 31); // 选择年份的最后一天
var currentDate = new Date(startDate);
while (currentDate <= endDate) {
var weekNumber = getWeekNumber(currentDate);
var option = document.createElement("option");
option.value = weekNumber;
option.text = "第" + weekNumber + "周 (" + formatDate(currentDate) + ")";
weekSelect.appendChild(option);
// 增加一周
currentDate.setDate(currentDate.getDate() + 7);
}
}
function getWeekNumber(date) {
var onejan = new Date(date.getFullYear(), 0, 1);
return Math.ceil(((date - onejan) / 86400000 + onejan.getDay() + 1) / 7);
}
function formatDate(date) {
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
return year + "-" + month + "-" + day;
}