自己封装的关于jquery easyUI的datetimebox开始时间不能大于结束时间的判断

来源:互联网 发布:透明图标软件 编辑:程序博客网 时间:2024/05/16 15:20

工作中遇到jquery easyUI中的时间没有属性判断时,自己封装了以下的方法:

1.首先定义两个相同的方法,方法名为formatDateTimeBox(startTimeId,endTimeId,true)和formatDateTimeBox(endTimeId,startTimeId,false);

2.参数介绍:startTimeId为时间查询框中开始时间的ID,endTimeId为结束时间的ID,第三个参数true or false判断先点击前面的时间框还是后面的时间查询框。

3.公共方法:文字不多说,直接上代码.

function formatDateTimeBox(frontId,lastId,flag){

 $("#"+frontId).datetimebox({
             formatter: function (date) {
                 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=$("#"+lastId).datetimebox("getValue").replaceAll("-","").replaceAll(" ","").replaceAll(":","");
if(flag==true){
if(parseInt(sTime)>parseInt(eTime)){
alert("提示", "开始时间不能大于结束时间!");
return "";
}else{
                   return y + "-" + (m < 10 ? ("0" + m) : m) + "-" + (d < 10 ? ("0" + d) : d)+" " + h+":"+minute+":"+s ;

}else{
if(parseInt(sTime)<parseInt(eTime)){
alert("提示", "开始时间不能大于结束时间!");
return "";
}else{
                   return y + "-" + (m < 10 ? ("0" + m) : m) + "-" + (d < 10 ? ("0" + d) : d)+" " + h+":"+minute+":"+s ;

}
                 
             },
             onSelect: function(date){
              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();
                return y + "-" + (m < 10 ? ("0" + m) : m) + "-" + (d < 10 ? ("0" + d) : d)+" "  + h+":"+minute+":"+s;
             }  
         });

}

需要注意的是js的replaceAll方法,需要自己在js里申明这样一个方法:

 String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) {  
            if (!RegExp.prototype.isPrototypeOf(reallyDo)) {  
                return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith);  
            } else {  
                return this.replace(reallyDo, replaceWith);  
            }  
        };  

0 0
原创粉丝点击