Below code is to remove sundays and holidays between two datetime.
Kindly review and suggest for efficient and accurate solution.
Note: I need to add one more case where if there is any sunday or holiday , the first 6 hours of next day should not be considered to idleTime.
Any suggestions would be helpful and appreciated.
var customFunction = function(startDate, endDate) {
//Holiday List
var holidayList = ["2023-08-15", "2023-09-18", "2023-10-02", "2023-10-24", "2023-11-01", "2023-11-13", "2024-01-06", "2024-01-15", "2024-01-26", "2024-04-06", "2024-04-09", "2024-05-01", "2024-05-04"]
if (startDate === 0) {
return 0
}
const oneHour = 60 * 60 * 1000;
//To increment the day
const oneDay = 24 * 60 * 60 * 1000;
const start = new Date(startDate);
const start2 = new Date(startDate);
const end = new Date(endDate);
var diffHours = (Math.abs(start - end) / oneHour).toFixed(3);
// console.log( "startstart",start,end)
let sundays = 0; //To store completed hours of sunday between two dates
let sundayHours = 0;
let endsundayHours = 0;
let sundayDates = [];
let startMonth = start.getMonth() + 1
let endMonth = end.getMonth() + 1
let formattedstartDate = start.toISOString().split('T')[0];
let formattedendtDate = end.toISOString().split('T')[0];
let formatedCurrentDate = 0;
//Holiday Days
let numberOfHolidays = 0;
let fullholidayHours = 0;
let holidayHours = 0;
let holidays = [];
if (!((start.getUTCDay() === 0 && end.getUTCDay() === 0) && (start.getUTCDate() === end.getUTCDate())) && !((start.getUTCDate() === end.getUTCDate) && (startMonth === endMonth)) && !(holidayList.includes(formattedstartDate) && holidayList.includes(formattedendtDate))) {
//#case 1
if (start.getUTCDay() === 0) {
sundayHours += 24 - (start.getUTCHours() + start.getUTCMinutes() / 60);
}
// holidayList.includes(formattedstartDate)
if (!(start.getUTCDay() === 0) && holidayList.includes(formattedstartDate)) {
holidayHours += 24 - (start.getUTCHours() + start.getUTCMinutes() / 60);
}
//#case 2 Iterate through each day between the two dates
for (let currentDate = start; currentDate <= end; currentDate.setTime(currentDate.getTime() + oneDay)) {
formatedCurrentDate = currentDate.toISOString().split('T')[0];
const month = currentDate.getUTCMonth();
const currentDateCopy = new Date(currentDate);
const x1 = new Date(
Date.UTC(
currentDateCopy.getUTCFullYear(),
month,
currentDateCopy.getUTCDate(),
0,
0,
0
)
);
const x2 = new Date(
Date.UTC(
currentDateCopy.getUTCFullYear(),
month,
currentDateCopy.getUTCDate(),
23,
59,
0
)
);
if (currentDate.getUTCDay() === 0 && start2 <= x1 && x2 <= end) {
const sundayDate = new Date(currentDate);
sundayDates.push(sundayDate);
sundays += 30; // Store the Sunday date
}
//isHoliday(formatedCurrentDate)
if (!(currentDate.getUTCDay() === 0) && holidayList.includes(formatedCurrentDate) && start2 <= x1 && x2 <= end) {
numberOfHolidays += 1;
const holidayDates = new Date(currentDate);
holidays.push(holidayDates)
fullholidayHours += 30;
}
}
//#case 3 Check if the end date is on a Sunday
if (end.getUTCDay() === 0) {
sundayIsPresent = end.getUTCDay()
endsundayHours += end.getUTCHours() + (end.getUTCMinutes() / 60);
}
// Check if the end date a holiday holidayList.includes(formattedendtDate)
if (!(end.getUTCDay() === 0) && holidayList.includes(formattedendtDate)) {
holidayHours += end.getUTCHours() + (end.getUTCMinutes() / 60);
}
} else if ((start.getUTCDay() === end.getUTCDay()) && (holidayList.includes(formattedstartDate) && holidayList.includes(formattedendtDate))) {
diffHours = 0
} else {
sundays = 0;
sundayHours = 0;
}
const finalValueInHours = sundayHours + parseFloat(diffHours) - endsundayHours - sundays - fullholidayHours - holidayHours;
const finalValueInMinutes = (sundayHours + parseFloat(diffHours) - endsundayHours - sundays - fullholidayHours - holidayHours) * 60;
return finalValueInMinutes;
}
const startTime = "2024-05-03T10:00:00Z"; // Start time in UTC
const endTime = "2024-05-06T12:30:00Z"; // End time in UTC
console.log(customFunction(startTime, endTime))
1