How to set minDate and maxDate for bootstrap datetimepicker on change

会有一股神秘感。 提交于 2019-12-09 01:16:27

问题


I want to ask users for a date range using bootstrap datetimepicker. There is a field for the start date and another one for the end date, and they're initialized to the day before current day. On change to the start date, I want to set the minDate of end date to the value of start date; and on change to end date I want to set the max date of start date to the value of end date. But I cannot get this working.

Here is the JS code:

var start = $('.start');
var end = $('.end');
var d = new Date();
var month = d.getMonth() + 1;
var day = d.getDate() - 1;
var year = d.getFullYear();

start.datetimepicker(
    {
        useCurrent: false,
        defaultDate: month + '-' + day + '-' + year + ' ' + '12:00AM',
        autoClose: true
     })
 .on('change', function (selected) {
    end.minDate(selected.?????????); // What should replace the question marks?
 });
end.datetimepicker(
    {
        useCurrent: false,
        defaultDate: month + '-' + day + '-' + year + ' ' + '11:59PM',
        autoClose: true
     })
 .on('change', function (selected) {
    start.maxDate(selected.???????); // What should replace the question marks?
 });

and the HTML:

<div class="col-sm-3">
    <div class="form-group">
        <h4>Start Date</h4>
        <div class="input-group date">
            <input type="text" data-data-fomat="MM-DD-YYY h:mm A" data-ng-model="startDate" id="Calendar1" class="start form-control" placeholder="Choose a date" />
            <span class="input-group-addon">
                <span class="glyphicon glyphicon-calendar"></span>
            </span>
        </div>
    </div>
</div>
<div class="col-sm-3">
    <div class="form-group">
        <h4>End Date</h4>
        <div class="input-group date">
            <input type="text" data-data-fomat="MM-DD-YYY h:mm A" data-ng-model="endDate" id="Calendar2" class="end form-control" placeholder="choose a date" />
            <span class="input-group-addon">
                <span class="glyphicon glyphicon-calendar"></span>
            </span>
        </div>
    </div>
</div>

I've found tickets about similar operation on datepicker, but that is done differently from datetimepicker so please don't mark duplicate unless you find similar question for datetimepicker.


回答1:


Based on the Linked Pickers example in the Bootstrap datetimepicker documentation, this should work.

Start:

 .on('dp.change', function (selected) {
    end.data("DateTimePicker").minDate(selected.date);
 });

End:

 .on('dp.change', function (selected) {
    start.data("DateTimePicker").maxDate(selected.date);
 });

Note:

On older versions of Bootstrap datetimepicker (less than v4), use .setMinDate and .setMaxDate




回答2:


  1. Start Date

    .on('dp.change', function (selected) {
        end.data("DateTimePicker").minDate(selected.date);
     });
    
  2. End Date

    .on('dp.change', function (selected) {
        start.data("DateTimePicker").maxDate(selected.date);
     });
    

Package Upgrade : The date timepicker package has been upgraded to latest version where we can use 'maxDate()' in place of 'setMaxDate()' and 'minDate()' inplace of 'setMinDate()' as the recent options consider even time in hrs and mins while providing maxDate/minDate option.




回答3:


Try to use:

$('.datepickerProjectDate').data("DateTimePicker").minDate('2018-09-15');


来源:https://stackoverflow.com/questions/26977783/how-to-set-mindate-and-maxdate-for-bootstrap-datetimepicker-on-change

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