计算两个日期之前的工作日时长(小时)

    //计算请假时长,验证时间格式
    function calctimelong(obj, guid) {
        var previous = parseFloat($('#TimeLong' + guid).val());
        $('#TimeLong' + guid).val('0');
        var leavestartdate = $('#LeaveStartDate' + guid).val().trim();
        var leaveenddate = $('#LeaveEndDate' + guid).val().trim();
        var leavestarttime = $('#LeaveStartTime' + guid).val().trim();
        var leaveendtime = $('#LeaveEndTime' + guid).val().trim();

        if (leavestartdate == '' || leaveenddate == '' || leavestarttime == '' || leaveendtime == '') return;
        if (!testTimeFormatter(leavestarttime) || !testTimeFormatter(leaveendtime)) {
            $('#LeaveStartTime' + guid).val('00:00');
            $('#LeaveEndTime' + guid).val('00:00');
            $('#TimeLong' + guid).val('0');
            $.messager.alert("提示", "时间格式不正确", "Info", function () {
            });
            return;
        }

        if (leavestarttime == '00:00') leavestarttime = "08:30";
        if (leaveendtime == '00:00') leaveendtime = "18:00";

        var dt1 = Date.parse(new Date((leavestartdate + " " + leavestarttime).replace(/-/g, "/")));
        var dt2 = Date.parse(new Date((leaveenddate + " " + leaveendtime).replace(/-/g, "/")));
        if (dt1.toString() == 'NaN' || dt2.toString() == 'NaN') {
            $('#LeaveStartTime' + guid).val('00:00');
            $('#LeaveEndTime' + guid).val('00:00');
            $('#TimeLong' + guid).val('0');
            $.messager.alert("提示", "时间格式不正确", "Info", function () { });
            return;
        }

        if (dt1 > dt2) {
            $('#LeaveEndDate' + guid).val('');
            $('#LeaveEndTime' + guid).val('00:00');
            $('#TimeLong' + guid).val('0');
            $.messager.alert("提示", "请假开始时间不能大于结束时间", "Info");
            return;
        }

        var s1 = Date.parse(new Date((leavestartdate + " 08:30:00").replace(/-/g, "/")));
        var s2 = Date.parse(new Date((leavestartdate + " 12:00:00").replace(/-/g, "/")));
        var s3 = Date.parse(new Date((leavestartdate + " 13:30:00").replace(/-/g, "/")));
        var s4 = Date.parse(new Date((leavestartdate + " 18:00:00").replace(/-/g, "/")));

        var e1 = Date.parse(new Date((leaveenddate + " 08:30:00").replace(/-/g, "/")));
        var e2 = Date.parse(new Date((leaveenddate + " 12:00:00").replace(/-/g, "/")));
        var e3 = Date.parse(new Date((leaveenddate + " 13:30:00").replace(/-/g, "/")));
        var e4 = Date.parse(new Date((leaveenddate + " 18:00:00").replace(/-/g, "/")));

        var workdays = '@strworkday'.split(',');
        var difdt = 0;//请假时长
        var fdifdt = 0;  //第一天时长
        var mdifdt = 0; //中间日期时长
        var edifdt = 0; //最后一天时长
        /*******************************计算请假时长(根据工作日表)*********************************/
        if (leavestartdate == leaveenddate) {
            if (workdays.indexOf(leavestartdate) >= 0) {//工作日
                if (dt1 < s1) dt1 = s1;
                else if (dt1 >= s2 && dt1 < s3) dt1 = s3;
                else if (dt1 > s4) dt1 = s4;
                if (dt2 < e1) dt2 = e1;
                else if (dt2 >= e2 && dt2 < e3) dt2 = e2;
                else if (dt2 > e4) dt2 = e4;
                fdifdt = dt2 - dt1;//时差毫秒
                if (dt1 <= s2 && dt2 >= e3) fdifdt = fdifdt - 1.5 * 60 * 60 * 1000;
            }
        } else if (datediff(leavestartdate, leaveenddate) == 1) {
            if (workdays.indexOf(leavestartdate) >= 0) {//第一天工作日
                if (dt1 < s1) dt1 = s1;
                else if (dt1 >= s2 && dt1 < s3) dt1 = s3;
                else if (dt1 > s4) dt1 = s4;
                fdifdt = s4 - dt1;//时差毫秒
                if (dt1 <= s2) fdifdt = fdifdt - 1.5 * 60 * 60 * 1000;
            }
            if (workdays.indexOf(leaveenddate) >= 0) {//最后一天工作日
                if (dt2 < e1) dt2 = e1;
                else if (dt2 >= e2 && dt2 < e3) dt2 = e2;
                else if (dt2 > e4) dt2 = e4;
                edifdt = dt2 - e1;//时差毫秒
                if (dt2 >= e3) edifdt = edifdt - 1.5 * 60 * 60 * 1000;
            }
        } else if (datediff(leavestartdate, leaveenddate) > 1) {
            if (workdays.indexOf(leavestartdate) >= 0) {//第一天工作日
                if (dt1 < s1) dt1 = s1;
                else if (dt1 >= s2 && dt1 < s3) dt1 = s3;
                else if (dt1 > s4) dt1 = s4;
                fdifdt = s4 - dt1;//时差毫秒
                if (dt1 <= s2) fdifdt = fdifdt - 1.5 * 60 * 60 * 1000;
            }
            if (workdays.indexOf(leaveenddate) >= 0) {//最后一天工作日
                if (dt2 < e1) dt2 = e1;
                else if (dt2 >= e2 && dt2 < e3) dt2 = e2;
                else if (dt2 > e4) dt2 = e4;
                edifdt = dt2 - e1;//时差毫秒
                if (dt2 >= e3) edifdt = edifdt - 1.5 * 60 * 60 * 1000;
            }

            var tmpdate = leavestartdate;
            for (var i = 0; i < datediff(leavestartdate, leaveenddate) - 1 ; i++) {
                tmpdate = new Date(new Date(tmpdate).getTime() + 24 * 60 * 60 * 1000);
                var year = tmpdate.getFullYear();
                var month = tmpdate.getMonth() + 1;
                var date = tmpdate.getDate();
                tmpdate = year + '-' + month + '-' + date;
                if (workdays.indexOf(tmpdate) >= 0) { mdifdt = mdifdt + 8; }
            }
            mdifdt = mdifdt * 60 * 60 * 1000;
        }


        /**************************************************************************/
        difdt = fdifdt + mdifdt + edifdt;//请假时长

        //计算时间差的毫秒数
        var hours = (difdt / (3600 * 1000)).toFixed(2);
        if (hours < 0) hours = 0.00;
        $('#TimeLong' + guid).val(hours);

        if (previous != hours) {
            //清空选择的时期和工时
            showstr = "<span>选择调休使用工时日期</span>";
            $('#sdate' + guid).val('');
            $('#edate' + guid).val('');
            $('#TimeAtDays' + guid).empty();
            $('#TimeAtDays' + guid).append(showstr);
            $('#chooselongtotal' + guid).val(0);
            $('#TimeAtDaysHidden' + guid).val('');
        }
    }

    //当请假事由发生变化时
    function onreasonchange(obj, guid) {
        if ($(obj).val() == '调休') {
            $('#TimeAtDays' + guid).css('display', 'block');
        } else {
            $('#TimeAtDays' + guid).css('display', 'none');
        }
    }

    //计算日期相差天数
    function datediff(d1, d2) {
        var day = 24 * 60 * 60 * 1000;
        try {
            var dateArr = d1.split("-");
            var checkDate = new Date();
            checkDate.setFullYear(dateArr[0], dateArr[1] - 1, dateArr[2]);
            var checkTime = checkDate.getTime();

            var dateArr2 = d2.split("-");
            var checkDate2 = new Date();
            checkDate2.setFullYear(dateArr2[0], dateArr2[1] - 1, dateArr2[2]);
            var checkTime2 = checkDate2.getTime();

            var cha = (checkTime2 - checkTime) / day;
            return cha;
        } catch (e) {
            return false;
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值