自动计算请假工时 排除周六周日

/**
 * 请假工时计算
 * starttime 开始时间 "2019-03-28 16:02", endtime 结束时间 "2019-03-29 16:02"
  */
function leaveTime(starttime, endtime) {
    var start = new Date(starttime); //start = Thu Mar 28 2019 16:02:00 GMT+0800 (中国标准时间)
    var end = new Date(endtime);
    var startHour = start.getHours(); //16
    var endHour = end.getHours();
    var startDate = start.setHours(0, 0, 0); //startDate = 1553702400000
    var endDate = end.setHours(0, 0, 0);
    var dayNum = parseInt(Math.abs(endDate - startDate) / 1000 / 60 / 60 / 24);
    var hour = 0;
    if (startHour < 9) {
        startHour = 9;
    }
    //如果开始时间大于18点,
    if (startHour > 18) {
        startDate += 60 * 60 * 24 * 1000;
        starHour = 9;
    }
    if (endHour < 9) {
        endDate -= 60 * 60 * 24 * 1000;
        endHour = 18;
    }
    if (endHour > 18) {
        endHour = 18;
    }
    if (startDate == endDate) {
        var date = new Date(startDate);
        var week = date.getDay();
        if (week != 0 && week != 6) {
            hour = endHour - startHour;
            if (hour > 0 && hour < 4)
                hour = 4;
            if (hour > 4)
                hour = 8;
        }
    } else {
        for (var i = 0; i <= dayNum; i++) {
            var DateTime = startDate + 60 * 60 * 24 * 1000*i
            var date = new Date(DateTime);
            var week = date.getDay();
            if (week != 0 && week != 6) {
                if (DateTime == startDate) {
                    var timehour = 18 - startHour;
                    if (timehour > 0 && timehour <= 4)
                        hour =+ 4;
                    if (timehour > 4)
                        hour =+ 8;
                } else if (DateTime > startDate && DateTime < endDate) {
                    hour += 8;
                } else if (DateTime == endDate){
                    var timehour = endHour - 9;
                    if (timehour > 0 && timehour <= 4)
                        hour += 4;
                    if (timehour > 4)
                        hour += 8;
                }
            }
        }
    }
    return hour / 8.0; 
}

 

上一篇:C++篇之成员函数注册为回调函数


下一篇:日期计算