js计算时间差

function calctimelong(obj, guid) {
    $('#TimeLong' + guid).val('0');
    var date = $('#OvertimeDate' + guid).val().trim();
    var workstart = $('#WorkStartTime' + guid).val().trim();
    var workend = $('#WorkEndTime' + guid).val().trim();
    var reststart = $('#RestStartTime' + guid).val().trim();
    var restend = $('#RestEndTime' + guid).val().trim();
    if (!testTimeFormatter(workstart) || !testTimeFormatter(workend) || !testTimeFormatter(reststart) || !testTimeFormatter(restend)) {
        $('#WorkStartTime' + guid).val('00:00');
        $('#WorkEndTime' + guid).val('00:00');
        $('#RestStartTime' + guid).val('00:00');
        $('#RestEndTime' + guid).val('00:00');
        $('#TimeLong' + guid).val('0');
        $.messager.alert("提示", "时间格式不正确", "Info", function () {
        });
        return;
    }

    if (date == '' || workstart == '' || workstart == '00:00' || workend == '' || workend == '00:00') return;

    var dt1 = Date.parse(new Date((date + " " + workstart).replace(/-/g, "/")));
    var dt2 = Date.parse(new Date((date + " " + workend).replace(/-/g, "/")));
    var rdt1 = Date.parse(new Date((date + " " + reststart).replace(/-/g, "/")));
    var rdt2 = Date.parse(new Date((date + " " + restend).replace(/-/g, "/")));
    if (dt1.toString() == 'NaN' || dt2.toString() == 'NaN' || rdt1.toString() == 'NaN' || rdt2.toString() == 'NaN') {
        $('#WorkStartTime' + guid).val('00:00');
        $('#WorkEndTime' + guid).val('00:00');
        $('#RestStartTime' + guid).val('00:00');
        $('#RestEndTime' + guid).val('00:00');
        $('#TimeLong' + guid).val('0');
        $.messager.alert("提示", "时间格式不正确", "Info", function () {
        });
        return;
    }

    //在比较时间大小之前,必须先算出正确的时间
    var cur1 = Date.parse(new Date((date + " 00:00:00").replace(/-/g, "/")));
    var cur2 = Date.parse(new Date((date + " 05:00:00").replace(/-/g, "/")));
    var cur3 = Date.parse(new Date((date + " 23:59:59").replace(/-/g, "/")));

    if (dt1 >= cur1 && dt1 < cur2 && dt2 >= cur1 && dt2 < cur2) {
        dt1 = dt1 - 1 * 24 * 60 * 60 * 1000;
        dt2 = dt2 - 1 * 24 * 60 * 60 * 1000;
    } else if (dt1 >= cur1 && dt1 < cur2) {
        if (workstart != "00:00" && workend != "00:00" && reststart != "00:00" && restend != "00:00") {
            $('#WorkEndTime' + guid).val('00:00');
            $.messager.alert("提示", "工作时间请选择一个工作日周期", "Info");
        }
    }
    if (dt1 >= cur2 && dt1 <= cur3 && dt2 >= cur1 && dt2 < cur2) {
        dt2 = dt2 + 1 * 24 * 60 * 60 * 1000;
    }

    if (rdt1 >= cur1 && rdt1 < cur2 && rdt2 >= cur1 && rdt2 < cur2) {
        rdt1 = rdt1 - 1 * 24 * 60 * 60 * 1000;
        rdt2 = rdt2 - 1 * 24 * 60 * 60 * 1000;
    } else if (rdt1 >= cur1 && rdt1 < cur2) {
        if (workstart != "00:00" && workend != "00:00" && reststart != "00:00" && restend != "00:00") {
            $('#WorkEndTime' + guid).val('00:00');
            $.messager.alert("提示", "休息请选择一个工作日周期", "Info");
        }
    }
    if (rdt1 >= cur2 && rdt1 <= cur3 && rdt2 >= cur1 && rdt2 < cur2) {
        rdt2 = rdt2 + 1 * 24 * 60 * 60 * 1000;
    }
    if (workstart != "00:00" && workend != "00:00") {
        if (dt1 > dt2) {
            $('#WorkStartTime' + guid).val('00:00');
            $('#WorkEndTime' + guid).val('00:00');
            $.messager.alert("提示", "加班开始时间不能大于结束时间", "Info");
            return;
        }
    }
    if (reststart != "00:00" && restend != "00:00") {
        if ((reststart != '00:00' && reststart != '') && (restend != '00:00' && restend != '')) {
            if (rdt1 > rdt2) {
                $('#RestStartTime' + guid).val('00:00');
                $('#RestEndTime' + guid).val('00:00');
                $.messager.alert("提示", "休息开始时间不能大于结束时间", "Info");
                return;
            }
        }
    }
    if (workstart != "00:00" && workend != "00:00" && reststart != "00:00" && restend != "00:00") {
        if ((reststart != '00:00' && reststart != '') || (restend != '00:00' && restend != '')) {
            if (dt1 > rdt1) {
                $('#RestStartTime' + guid).val('00:00');
                $('#RestEndTime' + guid).val('00:00');
                $.messager.alert("提示", "加班开始时间不能大于休息开始时间", "Info");
                return;
            }
            if (rdt2 > dt2) {
                $('#RestStartTime' + guid).val('00:00');
                $('#RestEndTime' + guid).val('00:00');
                $.messager.alert("提示", "休息结束时间不能大于加班结束时间", "Info");
                return;
            }
        }
    }

    var difdt4 = 0;
    if ((reststart != '00:00' && reststart != '') && (restend != '00:00' && restend != '')) {
        difdt4 = rdt2 - rdt1;//休息时长
    }
    var difdt3 = dt2 - dt1;//加班时长+休息时长

    //计算时间差的毫秒数
    var hours = ((difdt3 - difdt4) / (3600 * 1000)).toFixed(2);
    $('#TimeLong' + guid).val(hours);
}

你可能感兴趣的:(js)