🏖️ 年假計算器
根據香港僱傭條例準確計算年假天數
📋 香港僱傭條例年假規定
僱員在首12個月服務期間每工作滿一個月可享有一天年假,其後每工作滿一年可享有不少於7天至最多14天年假。
年假天數對照表:
@media (max-width: 768px) {
.calculator-grid {
grid-template-columns: 1fr !important;
}
}
// 設置默認日期
document.addEventListener(‘DOMContentLoaded’, function() {
const today = new Date();
const oneYearAgo = new Date(today);
oneYearAgo.setFullYear(today.getFullYear() – 1);
const endDateInput = document.getElementById(‘endDateCalc’);
const startDateInput = document.getElementById(‘startDateCalc’);
if (endDateInput && startDateInput) {
endDateInput.value = today.toISOString().split(‘T’)[0];
startDateInput.value = oneYearAgo.toISOString().split(‘T’)[0];
}
});
// 主要計算函數
function calculateLeave() {
const startDateInput = document.getElementById(‘startDateCalc’);
const endDateInput = document.getElementById(‘endDateCalc’);
const resultDiv = document.getElementById(‘resultDisplay’);
const mainResult = document.getElementById(‘mainResult’);
const detailBreakdown = document.getElementById(‘detailBreakdown’);
if (!startDateInput || !endDateInput) {
alert(‘無法找到日期輸入欄位,請重新整理頁面’);
return;
}
const startDate = new Date(startDateInput.value);
const endDate = new Date(endDateInput.value);
// 驗證輸入
if (!startDateInput.value || !endDateInput.value) {
alert(‘請填入完整的日期信息’);
return;
}
if (startDate >= endDate) {
alert(‘計算截止日期必須晚於入職日期’);
return;
}
// 計算服務期間
const timeDiff = endDate.getTime() – startDate.getTime();
const totalDays = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
const serviceYears = Math.floor(totalDays / 365.25);
const remainingDays = totalDays % 365.25;
const serviceMonths = Math.floor(remainingDays / 30.44);
// 計算年假天數
let annualLeaveDays = 0;
let calculationBasis = ”;
if (totalDays = 9) {
annualLeaveDays = 14;
calculationBasis = ‘服務滿9年或以上,享有14天年假(法定最高天數)’;
} else if (serviceYears >= 1) {
annualLeaveDays = yearMapping[serviceYears] || 7;
calculationBasis = `服務滿 ${serviceYears} 年,根據僱傭條例享有 ${annualLeaveDays} 天年假`;
} else {
annualLeaveDays = 7;
calculationBasis = ‘服務滿1年,享有7天年假’;
}
}
// 顯示結果
if (mainResult && detailBreakdown && resultDiv) {
mainResult.innerHTML = `年假天數:${annualLeaveDays} 天`;
detailBreakdown.innerHTML = `
📊 詳細計算資訊
`;
resultDiv.style.display = ‘block’;
// 平滑滾動到結果區域
setTimeout(() => {
resultDiv.scrollIntoView({ behavior: ‘smooth’, block: ‘nearest’ });
}, 100);
}
}
// 為移動設備優化的響應式處理
function handleResponsiveGrid() {
const grids = document.querySelectorAll(‘[style*=”grid-template-columns”]’);
if (window.innerWidth {
if (grid.style.gridTemplateColumns.includes(‘1fr 1fr’)) {
grid.classList.add(‘calculator-grid’);
}
});
}
}
// 監聽窗口大小變化
window.addEventListener(‘resize’, handleResponsiveGrid);
window.addEventListener(‘load’, handleResponsiveGrid);