Calculate the elapsed time between the start and end date in hours
function calculateElapsedTime(projectDetailObj) {
// Default working hours from 8 AM to 5 PM with a 1-hour lunch break from 12 PM to 1 PM
const WORKING_START_HOUR = 8;
const WORKING_END_HOUR = 17;
const LUNCH_START_HOUR = 12;
const LUNCH_END_HOUR = 13;
// Convert the start and end times to Date objects
const startTime = new Date(projectDetailObj.actualStartDateTime);
const endTime = new Date(projectDetailObj.actualEndDateTime);
// Helper function to get the start of the working hours for a given day
function getWorkingStartOfDay(date) {
const newDate = new Date(date);
newDate.setHours(WORKING_START_HOUR, 0, 0, 0); // Set to 8:00 AM of the same day
return newDate;
}
// Helper function to get the end of the working hours for a given day
function getWorkingEndOfDay(date) {
const newDate = new Date(date);
newDate.setHours(WORKING_END_HOUR, 0, 0, 0); // Set to 5:00 PM of the same day
return newDate;
}
// Helper function to check if the date is a weekend (Saturday or Sunday)
function isWeekend(date) {
const dayOfWeek = date.getDay(); // 0 = Sunday, 6 = Saturday
return dayOfWeek === 0 || dayOfWeek === 6; // Return true if weekend
}
// Helper function to calculate working hours for a single day excluding lunch break
function getWorkingHours(start, end) {
const workdayStart = getWorkingStartOfDay(start);
const workdayEnd = getWorkingEndOfDay(start);
// If the start and end are within the same day
if (start >= workdayStart && end <= workdayEnd) {
// Exclude lunch break from 12 PM to 1 PM (1 hour)
let totalWorkingHours = (end – start) / (1000 * 60 * 60); // Convert ms to hours
// If the work period overlaps with lunch break, exclude the lunch hour
if (start < new Date(start).setHours(LUNCH_END_HOUR, 0, 0, 0) && end > new Date(start).setHours(LUNCH_START_HOUR, 0, 0, 0)) {
totalWorkingHours -= 1; // Subtract 1 hour for lunch break
}
return totalWorkingHours;
}
return 0; // No working hours if the start/end times are outside working hours
}
let totalHours = 0;
// Ensure that if the start time is before 8:00 AM, it adjusts to 8:00 AM
if (startTime.getHours() < WORKING_START_HOUR) {
startTime.setHours(WORKING_START_HOUR, 0, 0, 0); // Adjust start to 8:00 AM
}
// Ensure that if the end time is after 5:00 PM, it adjusts to 5:00 PM
if (endTime.getHours() >= WORKING_END_HOUR) {
endTime.setHours(WORKING_END_HOUR, 0, 0, 0); // Adjust end to 5:00 PM
}
// If the start time is after the end time, return 0 hours
if (startTime >= endTime) {
return 0;
}
// Loop through each day from start time to end time
while (startTime < endTime) {
const workingStartOfDay = getWorkingStartOfDay(startTime);
const workingEndOfDay = getWorkingEndOfDay(startTime);
// Skip weekends
if (isWeekend(startTime)) {
startTime.setDate(startTime.getDate() + 1); // Move to next day
startTime.setHours(WORKING_START_HOUR, 0, 0, 0); // Set to 8 AM next day
continue; // Skip the weekend day
}
// If the start time is on the same day, calculate the hours for that day
if (startTime < workingEndOfDay) {
const effectiveStart = startTime > workingStartOfDay ? startTime : workingStartOfDay;
const effectiveEnd = endTime < workingEndOfDay ? endTime : workingEndOfDay;
const dayHours = getWorkingHours(effectiveStart, effectiveEnd);
totalHours += dayHours;
}
// Move to the next day (8 AM) if we’re not at the end time yet
startTime.setDate(startTime.getDate() + 1);
startTime.setHours(WORKING_START_HOUR, 0, 0, 0);
}
// Round the total hours to 1 decimal place if there is a decimal
if (totalHours % 1 !== 0) {
return Math.round(totalHours * 10) / 10; // Round to 1 decimal place
}
return totalHours; // Return total hours if it’s an integer
}