开始日期不能大于结束日期 联动校验

1,html

                    <tr>
                        <td align="right">开始日期</td>
                        <td><input class="easyui-datetimebox" name="bb" id="bb"
                                   data-options="required:true,editable:false" style="width:180px"></td>
                    </tr>
                    <tr>
                        <td align="right">结束日期</td>
                        <td><input class="easyui-datetimebox" name="ee" id="ee"
                                   data-options="required:true,readonly:true,editable:false" style="width:180px"></td>
                    </tr>

2,初始化方法

    $(document).ready(function () {
        formatDateTimeBox('bb', 'ee', true)
        formatDateTimeBox('ee', 'bb', false)
    });

3,方法调用

function formatDateTimeBox(sId, eId, flag) {
        $("#xxForm #" + sId).datetimebox({
            formatter: function (date) {
                debugger
                var y = date.getFullYear();
                var m = date.getMonth() + 1;
                var d = date.getDate();
                var h = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
                var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
                var s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
                sTime = y + "-" + (m < 10 ? ("0" + m) : m) + "-" + (d < 10 ? ("0" + d) : d) + " " + h + ":" + minute + ":" + s;
                sTime = sTime.replaceAll("-", "").replaceAll(" ", "").replaceAll(":", "");
                eTime = $("#xxForm #" + eId).datetimebox("getValue").replaceAll("-", "").replaceAll(" ", "").replaceAll(":", "");
                if (flag == true) {
                    if (parseInt(sTime) > parseInt(eTime)) {
                        $.messager.alert("提示", "开始时间不能大于结束时间!");
                        return "";
                    } else {
                        return y + "-" + (m < 10 ? ("0" + m) : m) + "-" + (d < 10 ? ("0" + d) : d) + " " + h + ":" + minute + ":" + s;
                    }
                } else {
                    if (parseInt(sTime) < parseInt(eTime)) {
                        $.messager.alert("提示", "开始时间不能大于结束时间!");
                        return "";
                    } else {
                        return y + "-" + (m < 10 ? ("0" + m) : m) + "-" + (d < 10 ? ("0" + d) : d) + " " + h + ":" + minute + ":" + s;
                    }
                }
            },
            onSelect: function (date) {
                debugger
                var y = date.getFullYear();
                var m = date.getMonth() + 1;
                var d = date.getDate();
                var h = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
                var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
                var s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
                if(sId == 'ee'){
                    debugger
                    var canyuEndDate = y + "-" + (m < 10 ? ("0" + m) : m) + "-" + (d < 10 ? ("0" + d) : d);
                    //后去endDate
                    var endDate = $("#xxForm #bb").datebox("getValue");
                    //endDate = endDate.substr(0, 10);
                    // if (dateNumber(canyuEndDate) >= dateNumber(endDate) && dateNumber(endDate) != 0) {
                    var canyuEndDateNum = dateNumber(canyuEndDate);
                    var endDateStr = getNextDate(endDate,0);
                    var endDateNum = dateNumber(endDateStr);
                    if (dateNumber(endDate) != 0 && (canyuEndDateNum != endDateNum)) {
                        $.messager.alert("提示","两数相等");
                        $("#xxForm #ee").datebox("setValue", "")
                        return false;
                    }
                }

                return y + "-" + (m < 10 ? ("0" + m) : m) + "-" + (d < 10 ? ("0" + d) : d) + " " + h + ":" + minute + ":" + s;
            }
        });
    }

4,工具类方法

    //日期转换
    function dateNumber(param) {
        param = param.replace(/-/g, '');
        param = Number(param);
        return param;
    }

    // date 代表指定的日期,格式:2018-09-27
    // day 传-1表始前一天,传1表始后一天
    // JS获取指定日期的前一天,后一天
    function getNextDate(date, day) {
        var dd = new Date(date);
        dd.setDate(dd.getDate() + day);
        var y = dd.getFullYear();
        var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
        var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
        return y + "-" + m + "-" + d;
    };

 

上一篇:php date 时间差


下一篇:【LeetCode】841. 钥匙和房间