SOURCE

function calculateEffectiveWorkDays(startTime, endTime) {
            // 确保参数是有效的Date对象
            if (!(startTime instanceof Date) || !(endTime instanceof Date)) {
                throw new Error('Both startTime and endTime must be Date objects.');
            }

            var difference_in_time = endTime.getTime() - startTime.getTime(); 
      
            // To calculate the no. of days between two dates 
            var difference_in_days = difference_in_time / (1000 * 3600 * 24); 

            console.log("相差天数:" + difference_in_days)

            let totalEffectiveHours = 0;
            let currentDate = new Date(startTime);

            // 遍历每个工作日(从开始日期的下一个工作日开始,到结束日期的下一个工作日结束)
            for (let i = 0; i < difference_in_days + 1; i++) {
                // 获取当前工作日的开始和结束时间
                let workDayStart = new Date(currentDate);
                if(workDayStart.getHours() < 9) {
                    workDayStart.setHours(9, 0, 0, 0);
                }

                let workDayEnd = new Date(currentDate);
                
                if(endTime.getHours() > 18) {
                    workDayEnd.setHours(18, 0, 0, 0);
                } else {
                    
                    workDayEnd.setHours(endTime.getHours(), endTime.getMinutes(), endTime.getSeconds(), endTime.getMilliseconds());
                    //workDayEnd.setHours(18, 0, 0, 0);
                }
              
                // 如果当前工作日的结束时间超过了结束日期,则将其设置为结束日期
                if (workDayEnd > endTime) {
                    workDayEnd = new Date(endTime);
                }

                // 计算当前工作日的有效工作时间
                let effectiveHoursOnDay = (workDayEnd - workDayStart) / (1000 * 60 * 60);
                console.log("有效工时 for " + i + ":"+ effectiveHoursOnDay)
                if(effectiveHoursOnDay > 0 && effectiveHoursOnDay <= 4) {
                    effectiveHoursOnDay = 4
                } else if(effectiveHoursOnDay > 4){
                    effectiveHoursOnDay = 8
                }
            
                console.log("有效工时 for  " + i + ":"+  totalEffectiveHours)
                totalEffectiveHours += effectiveHoursOnDay;

                // 更新当前日期为下一个工作日
                currentDate.setDate(currentDate.getDate() + 1);
                // 如果当前日期超过了结束日期,则退出循环
                if (currentDate > endTime) {
                    break;
                }
            }

            // 根据规则计算有效工时对应的天数
            console.log("有效工时"+ totalEffectiveHours)
            let workDays = Math.floor(totalEffectiveHours / 8); // 一天工作8小时
            console.log("有效工时"+ totalEffectiveHours + ",计算出的天数:" + workDays + ",余数:" + totalEffectiveHours % 8)
            if (totalEffectiveHours % 8 > 4) {
                workDays += 1; // 如果剩余时间大于等于4小时,则加半天
            } else if(totalEffectiveHours % 8 <= 4 && totalEffectiveHours % 8 > 0) {
                workDays += 0.5;
            }
            return workDays;
        }
// 示例使用
        let startTime = new Date("2024-03-28 16:20"); // 2023年4月1日 10:00:00
        let endTime = new Date("2024-03-29 16:20"); // 2023年4月3日 16:30:00

        let effectiveWorkDays = calculateEffectiveWorkDays(startTime, endTime);
        console.log(effectiveWorkDays); 
console 命令行工具 X clear

                    
>
console