我正在尝试使用disabledTimeIntervals
禁用今天的未来时间,但它似乎无法正常工作。
我需要做的是暂停今天的时间选择器的未来时间,因为我可以使用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/
答案 0 :(得分:1)
您可以使用maxTime
选项和函数onChangeDateTime
根据所选日期设置minTime。
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">
希望有所帮助!
答案 1 :(得分:0)
您在jsfiddle中使用的datetimepicker没有disabledTimeIntervals
选项,这就是它无效的原因。你应该使用disabledMinTime
&amp; disabledMaxTime
至于解决方案,我猜你想要禁用今天的所有未来时间,但仍希望在所有可能的时间内允许未来的日期。这是一个相同的jsfiddle: -
https://jsfiddle.net/sahilbatla/zpzL2po3/
代码如下所示: -
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})
}
}
});