Search code examples
jquerydatepicker

How to make a datepicker choose only from a day after current day


I used the following jQuery code to get the dates into my input fields in order to have a "from" and "to" date inputs.

$("#dt1").datepicker({
    dateFormat: "dd-M-yy",
    minDate: 0,
    onSelect: function (date) {
        var dt2 = $('#dt2');
        var startDate = $(this).datepicker('getDate');
        var minDate = $(this).datepicker('getDate');
        dt2.datepicker('setDate', minDate);
        startDate.setDate(startDate.getDate() + 360);
        //sets dt2 maxDate to the last day of 30 days window
        dt2.datepicker('option', 'maxDate', startDate);
        dt2.datepicker('option', 'minDate', minDate);
        $(this).datepicker('option', 'minDate', minDate);
    }
});
$('#dt2').datepicker({
    dateFormat: "dd-M-yy"
});

If I choose the first date as 19-04-2017, automatically the second date starts from 19-04-2017 only. Question is: How can I make my second date only start counting one day after the first chosen date?

Meaning it would be 20-04-2017 instead of 19...

Here you can see my entire fiddle

Hope you can help.


Solution

  • You need only this (change onSelect like given below):-

    onSelect: function (date) {
        var dt2 = $('#dt2');
        var startDate = $(this).datepicker('getDate','+1d');
        startDate.setDate(startDate.getDate()+1); 
        dt2.datepicker('option', 'minDate', startDate);
        dt2.datepicker('setDate', startDate);
    }