Jquery - DateTimePicker set max datetime

你说的曾经没有我的故事 提交于 2019-12-25 06:59:29

问题


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 is2016/05/11 23:55then max ofTowould be2016/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

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