How to deal with holidays
Get the total of working days comparing two dates:
function isHoliday(dateToCheck, holidayList) {
const formattedDateToCheck = dateToCheck.toISOString().split('T')[0];
return holidayList.some(holiday => holiday === formattedDateToCheck);
}
function isWeekend(date) {
const day = date.getDay();
return day === 0 || day === 6; // Sunday (0) or Saturday (6)
}
function getWorkingDays(startDate, endDate, holidays) {
let count = 0;
let currentDate = new Date(startDate);
// Loop through each day between startDate and endDate
while (currentDate <= endDate) {
// Check if the current date is not a weekend and not a holiday
if (!isWeekend(currentDate) && !isHoliday(currentDate, holidays)) {
count++;
}
// Move to the next day
currentDate.setDate(currentDate.getDate() + 1);
}
return count;
}
Example
// Example usage:
const holidays = [
"2024-01-01", // New Year's Day
"2024-12-25", // Christmas
"2024-07-04", // Independence Day
"2024-11-28" // Thanksgiving
];
const startDate = new Date("2024-08-19");
const endDate = new Date("2024-08-30");
console.log(getWorkingDays(startDate, endDate, holidays)); // Output will depend on the date range and holidays
Please give details of the problem