问题
I have 2 datetimepicker. 1 is From
, another is To
. how to I limit my To
datetimepicker to be 30 minutes after From
?
I know how to limit the maximum date, but I dont know how to limit the maximum date and time.
for example :
If From
is 2016/05/11 00:00
then max of To
would be 2016/05/11 00:30
But, if From is
2016/05/11 23:55then max of
Towould be
2016/05/12 00:25`
my current code :
$("#From").datetimepicker({
onClose: function (selectedDate, instance) {
if (selectedDate != '') {
$("#To").datetimepicker("option", "minDate", selectedDate);
var date = $.datepicker.parseDateTime("dd/mm/yy", "hh:mm", selectedDate);
date.setMinutes(date.getMinutes() + 30);
$("#To").datetimepicker("option", "maxTime", selectedDate);
}
}
});
$("#To").datetimepicker();
this code is not working, and Im at a loss. Should I set the maxDate
and maxTime
separately?
回答1:
you can use e.date
which is a Moment instance, then you can easily do e.date.add(30, 'minutes')
, like this:
var fromDP = $("#From").datetimepicker();
var toDP = $("#To").datetimepicker();
fromDP.on("dp.change", function(e){
var newDate = e.date.add(30, 'minutes');
toDP.data("DateTimePicker").maxDate(newDate);
});
working example HERE
..and if you want to set it as default date aswell you can do
toDP.data("DateTimePicker").date(newDate).maxDate(newDate);
来源:https://stackoverflow.com/questions/37156602/jquery-datetimepicker-set-max-datetime