Search code examples
jquerybootstrap-datetimepickereonasdan-datetimepicker

Not able to set older date then current if minDate property is set to moment() using Bootstrap Datetimepicker


My requirement is i don't want allow user to select past date from the date picker (this was done using minDate: moment()). But i must have to show the past date in textbox as well as in datepicker dropdown.

For example, Today is 23 Oct 2017, So i am able to select any date from today and future. But few dates that were already in past (i.e. 03 Mar 2017, 12 Nov 2016) in that case those dates were not shown in the textbox it was automatically cleared due to minDate: moment() feature is set. If i remove this property then i am able to show that date.

This issue i was facing after migrating Bootstrap Version 3.0.0 to 4.17.42. Following is the code:

$('#datepicker').datetimepicker({
        format: 'YYYY/MM/DD',
        ignoreReadonly: true,
        defaultDate: moment(GetCurrentDate(), "DD-MM-YYYY"),
        minDate: moment()           //set a minimum date
});

Using this DateTimePicker: http://eonasdan.github.io/bootstrap-datetimepicker/


Solution

  • For this you must set as option keepInvalid: true as by default keepInvalid is false, that way the minDate option won't overwrite the date before.

    This way any Date that was before on the picker should stay there.

    So your code should look like:

    $('#datepicker').datetimepicker({
        format: 'YYYY/MM/DD',
        ignoreReadonly: true,
        defaultDate: moment(GetCurrentDate(), "DD-MM-YYYY"),
        minDate: moment(),           //set a minimum date
        keepInvalid: true,
    });
    

    Now in case you're not using minDate option you must set useCurrent: false that way won't be problems.

    This issue i was facing after migrating Bootstrap Version 3.0.0 to 4.17.42

    I advise you to upgrade to the latest Version 4.17.47 some fixes has been made