wdatepicker,自定义时间限制

来源:互联网 发布:华为软件下载官方网站 编辑:程序博客网 时间:2024/04/30 19:26

近来做项目,遇到一个需求:填写单子时,只能填写7天以内,不能填写现在之前的,而且时间间隔不能超过1天,

查看了Wdatepicker的官网http://www.my97.net/dp/demo/index.htm,发现不能原有的方法都不能满足这个需求,就自定义了方法,来满足

时间样式:


jsp代码:

<span style="font-size:18px;"><input type="text" class="date-txt" id="startTime" readOnly="true" name="startTime"  value=""
onClick="WdatePicker({minDate:startMinDate(),maxDate:startMaxDate(),dateFmt:'yyyy-MM-dd HH:mm'})" nullmsg="值班时间不能为空" datatype="*" /> 至 <input type="text" class="date-txt" id="endTime" readOnly="true" name="endTime"  value="" 
<span style="white-space:pre"></span>onClick="WdatePicker({minDate:endMinDate(),maxDate:endMaxDate(),dateFmt:'yyyy-MM-dd HH:mm'})" nullmsg="值班时间不能为空" datatype="*"/></span>
自定义的方法:

<span style="font-size:18px;">//当前时间去除秒的毫秒数//使用“.replace('-','/')”是为了兼容IEvar now = Date.parse(new Date().format("yyyy-MM-dd hh:mm").replace('-','/'));/** * 开始时间的最小时间 *  */var startMinDate = function(){var endTime = $("#endTime").val();var time = Date.parse(endTime.replace('-','/'));//结束时间不能超过当前时间+7天if(endTime!=""&&(time-7*24*60*60*1000)>now){return new Date(now+7*24*60*60*1000).format("yyyy-MM-dd hh:mm");}//结束时间不能小于当前时间,诺小于则返回当前时间if(endTime!=""&&(time-1*24*60*60*1000)<=now){return new Date(now).format("yyyy-MM-dd hh:mm");}if(endTime==""){return new Date(now).format("yyyy-MM-dd hh:mm");}else{return new Date(time-1*24*60*60*1000).format("yyyy-MM-dd hh:mm");}}/** * 开始时间的最大时间 */var startMaxDate = function(){var endTime = $("#endTime").val();var time = Date.parse(endTime.replace('-','/'));//结束时间不能超过当前时间+7天if(endTime!=""&&(time-7*24*60*60*1000)>now){return new Date(now+7*24*60*60*1000).format("yyyy-MM-dd hh:mm");}//结束时间不能小于当前时间,诺小于则返回当前时间if(endTime!=""&&(time-1*24*60*60*1000)<=now){return new Date(time).format("yyyy-MM-dd hh:mm");}if(endTime==""){return new Date(now+7*24*60*60*1000).format("yyyy-MM-dd hh:mm");}else{return new Date(time).format("yyyy-MM-dd hh:mm");}}/** * 结束时间的最小时间 */var endMinDate = function(){var startTime = $("#startTime").val();var time = Date.parse(startTime.replace('-','/'));//开始时间不能大于当前时间+7天if(startTime!=""&&(time-7*24*60*60*1000)>=now){return new Date(now+7*24*60*60*1000).format("yyyy-MM-dd hh:mm");}//开始时间不能小于当前时间if(startTime!=""&&time<now){return new Date(now).format("yyyy-MM-dd hh:mm");}if(startTime==""){return new Date(now).format("yyyy-MM-dd hh:mm");}else{return new Date(time).format("yyyy-MM-dd hh:mm");}}/** * 结束时间的最大时间 */var endMaxDate= function(){var startTime = $("#startTime").val();var time = Date.parse(startTime.replace('-','/'));//开始时间不能大于当前时间+7天if(startTime!=""&&(time-7*24*60*60*1000)>=now){return new Date(now+7*24*60*60*1000).format("yyyy-MM-dd hh:mm");}//开始时间不能小于当前时间if(startTime!=""&&time<now){return new Date(time+1*24*60*60*1000).format("yyyy-MM-dd hh:mm");}if(startTime==""){return new Date(now+7*24*60*60*1000).format("yyyy-MM-dd hh:mm");}else{return new Date(time+1*24*60*60*1000).format("yyyy-MM-dd hh:mm");}}</span>


0 0
原创粉丝点击