const CountdownTimer = (() => { const holidays = [ { name: "元旦", date: (year) => new Date(year, 0, 1) }, { name: "春节", date: getChineseNewYear }, // 需要农历转换 { name: "清明节", date: getQingmingFestival }, { name: "五一", date: (year) => new Date(year, 4, 1) }, { name: "端午节", date: getDragonBoatFestival }, { name: "中秋", date: getMidAutumnFestival }, { name: "国庆节", date: (year) => new Date(year, 9, 1) } ]; function getChineseNewYear(year = new Date().getFullYear()) { // 示例:使用农历转换库获取春节日期(可替换为你的算法或数据表) // 这里是硬编码示例年份 const lunarNewYears = { 2024: '2024-02-10', 2025: '2025-01-29', 2026: '2026-02-17', 2027: '2027-02-06' }; return new Date(lunarNewYears[year] || lunarNewYears[year + 1]); } function getQingmingFestival(year = new Date().getFullYear()) { // 清明节一般为4月4日或5日 return new Date(year, 3, 4); // 公历 4 月 4 日 } function getDragonBoatFestival(year = new Date().getFullYear()) { const lunar = { 2024: '2024-06-10', 2025: '2025-05-31', 2026: '2026-06-20' }; return new Date(lunar[year] || lunar[year + 1]); } function getMidAutumnFestival(year = new Date().getFullYear()) { const lunar = { 2024: '2024-09-17', 2025: '2025-10-06', 2026: '2026-09-26' }; return new Date(lunar[year] || lunar[year + 1]); } function findNearestHoliday() { const now = new Date(); let nearest = null; let minDiff = Infinity; for (let holiday of holidays) { let date = holiday.date(now.getFullYear()); if (date < now) { date = holiday.date(now.getFullYear() + 1); } const diff = date - now; if (diff < minDiff) { minDiff = diff; nearest = { name: holiday.name, date }; } } return nearest; } const config = { targetName: "节日", units: { day: { text: "今日", unit: "小时" }, week: { text: "本周", unit: "天" }, month: { text: "本月", unit: "天" }, year: { text: "本年", unit: "天" } } }; const calculators = { day: () => { const hours = new Date().getHours(); return { remaining: 24 - hours, percentage: (hours / 24) * 100 }; }, week: () => { const day = new Date().getDay(); const passed = day === 0 ? 6 : day - 1; return { remaining: 6 - passed, percentage: ((passed + 1) / 7) * 100 }; }, month: () => { const now = new Date(); const total = new Date(now.getFullYear(), now.getMonth() + 1, 0).getDate(); const passed = now.getDate() - 1; return { remaining: total - passed, percentage: (passed / total) * 100 }; }, year: () => { const now = new Date(); const start = new Date(now.getFullYear(), 0, 1); const total = 365 + (now.getFullYear() % 4 === 0 ? 1 : 0); const passed = Math.floor((now - start) / 86400000); return { remaining: total - passed, percentage: (passed / total) * 100 }; } }; function updateCountdown() { const elements = ['eventName', 'eventDate', 'daysUntil', 'countRight'] .map(id => document.getElementById(id)); if (elements.some(el => !el)) return; const [eventName, eventDate, daysUntil, countRight] = elements; const now = new Date(); const { name, date } = findNearestHoliday(); // 获取最近节日 eventName.textContent = name; eventDate.textContent = date.toISOString().split('T')[0]; daysUntil.textContent = Math.ceil((date - new Date(now.getFullYear(), now.getMonth(), now.getDate())) / 86400000); countRight.innerHTML = Object.entries(config.units) .map(([key, { text, unit }]) => { const { remaining, percentage } = calculators[key](); return `