Bootstrap datetimepicker disable future date and time

余生颓废 提交于 2019-12-04 07:02:34

问题


I am trying to disable future hours within today using disabledTimeIntervals but it doesn't seem to work.

What I need to do is disable future hours in timepicker for today only, because disabling dates I can do with maxDate.

<input class="form-control input-sm pull-left " id="dp" placeholder="To" type="text">
$('#dp').datetimepicker({
  maxDate: '0',
  disabledTimeIntervals: [[moment(), moment().hour(24).minutes(0).seconds(0)]],
  format: 'm/d/Y H:i A',
  timepicker: true,
});

JSFIDDLE: https://jsfiddle.net/3oxmccjq/1/


回答1:


You can use maxTime option and function onChangeDateTime to set the minTime according to the selected date.

  • The comparison between dates is up to you :-)

var today = new Date();

var options = {
  maxDate: new Date(),
  maxTime: new Date(),
  disabledTimeIntervals: [
    [moment(), moment().hour(24).minutes(0).seconds(0)]
  ],
  format: 'm/d/Y H:i A',
  timepicker: true,
  onChangeDateTime: function(date) {
    // Here you need to compare date! this is up to you :-)
    if (date.getDate() === today.getDate()) {
      this.setOptions({maxTime: new Date()});
    } else {
      this.setOptions({maxTime: false});
    }
  }
};

$('#dp').datetimepicker(options);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.4/build/jquery.datetimepicker.full.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.4/jquery.datetimepicker.css" rel="stylesheet" />

<input class="form-control input-sm pull-left " id="dp" placeholder="To" type="text">

Hope it helps!




回答2:


The datetimepicker you are using in your jsfiddle have no options as disabledTimeIntervals and that's why it is not working. You should use disabledMinTime & disabledMaxTime

As for the solution I am guessing you want to disable all future hours for today but still want to allow future dates with all the possible time. Here is a jsfiddle for same :-

https://jsfiddle.net/sahilbatla/zpzL2po3/

Code looks like this :-

var today = new Date();
var todayEndOfDay = new Date().setHours(23,59,59,999);
$('#dp').datetimepicker({ 
  disabledMinTime: today,
  disabledMaxTime: todayEndOfDay,
  format: 'm/d/Y H:i A',
  timepicker: true,
  onChangeDateTime: function(date) {
    if (date.getDate() !== today.getDate()) {
      this.setOptions({disabledMinTime: false, disabledMaxTime: false}) 
    } else {
      this.setOptions({disabledMinTime: today, disabledMaxTime: todayEndOfDay})
    }
  }
});


来源:https://stackoverflow.com/questions/48428145/bootstrap-datetimepicker-disable-future-date-and-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!