1

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

Ivan
  • 34,531
  • 8
  • 55
  • 100
Sam1486
  • 11
  • 2
  • 1
    First google search I did: [Jquery UI datepicker. Disable array of Dates](https://stackoverflow.com/questions/15400775/jquery-ui-datepicker-disable-array-of-dates) – Granny Sep 07 '17 at 12:16
  • Ah yes thank you Ivan I came across that post, but was unable to implement it into my existing code - which has the other functionality included already, could you be so kind to help me add this? – Sam1486 Sep 07 '17 at 12:29

1 Answers1

1

$('#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.

Durga
  • 15,263
  • 2
  • 28
  • 52