js 계산 시차

6157 단어 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);
}

좋은 웹페이지 즐겨찾기