Sam1486
Sam1486

Reputation: 11

Date Picker - disabling a range of dates in the future

I have been trying to implement some additional code to disable a range of dates in the future on my date range picker, from my research I believe I need to use beforeShowDay, have the dates in an array and return a false if the dates are in the array but each time I try to spanner in some code it breaks the pickers. I have to confess I don't know Javascript very well at all and only use it with plugins when I have to, any help would be gratefully received.

var dateToday = new Date();
var dates = $("#from, #to").datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1,
minDate: dateToday,
onSelect: function(selectedDate) {
    var option = this.id == "from" ? "minDate" : "maxDate",
        instance = $(this).data("datepicker"),
        date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
    dates.not(this).datepicker("option", option, date);
}
});

I would like to disable from the 2nd of December 2017 to the January 27th 2018

Upvotes: 1

Views: 2006

Answers (1)

Durga
Durga

Reputation: 15604

$('#datePick').datepicker({
    defaultDate: "+1w",
    changeMonth: true,
    numberOfMonths: 1,
    minDate: new Date(),
    beforeShowDay: function(date){
        var val = new Date("2017-12-02") >= date || new Date("2018-01-28") < date ;
        return [ val ]
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/themes/redmond/jquery-ui.css" rel="stylesheet"/>

// disabled 2nd of December 2017 to  January 27th 2018<br>
<input id='datePick'>

use beforeShowDay and check which date you need to show.

Upvotes: 1

Related Questions