datepicker控件设置开始时间,结束时间自动默认加一年

牛昱
2023-12-01

需引用文件:
/bootstrap-datepicker3.min.css
/datepicker/bootstrap-datepicker.min.js
/datepicker/locale/zh-cn.js"

html代码部分:

<input name="maintenstarttime" id="maintenstarttime" type="text" class="form-control input-date">
<input name="maintenendtime" id="maintenendtime" type="text" class="form-control input-date">

js代码部分:

 $('#maintenstarttime').datepicker({
     autoclose: true,
     todayHighlight: true,
     format: "yyyy-mm-dd",
     language: "zh-cn",
 }).on('changeDate',function(e){
     if (e.date) {
       $('#maintenendtime').datepicker('setStartDate', new Date(e.date.valueOf()));
     } else {
       $('#maintenendtime').datepicker('setStartDate', null);
     }
     if($('#maintenendtime').val() == ""){
       var time = new Date(e.date.valueOf()+24*60*60*365*1000);
       var y = time.getFullYear();
       var m = (time.getMonth() + 1 < 10) ? ("0" + (time.getMonth() + 1)) : (time.getMonth() + 1);
       var d = (time.getDate() < 10) ? ("0" + time.getDate()) : time.getDate();
       $('#maintenendtime').val(y+"-"+m+"-"+d);
     }
});

$('#maintenendtime').datepicker({
     autoclose: true,
     todayHighlight: true,
     format: "yyyy-mm-dd",
     language: "zh-cn",
}).on('changeDate',function(e){
   if (e.date) {
       $('#maintenstarttime').datepicker('setEndDate', new Date(e.date.valueOf()));
   } else {
       $('#maintenstarttime').datepicker('setEndDate', null);
   }
});

 类似资料: