Home > Software engineering >  How do I disable both weekends and selected dates in jquery ui date picker?
How do I disable both weekends and selected dates in jquery ui date picker?

Time:09-18

ideally the dates array will check my google calendar for dates and I want to block those dates in my calendar

<script> $(document).ready(function(){

    var dates = ["06-08-2021"];
 
    function DisableDates(date) {
    var string = jQuery.datepicker.formatDate('mm-dd-yy', date);
    return [dates.indexOf(string) == -1];
}
 


    $("#datepicker").datepicker({
        dateFormat: 'mm-dd-yy', 
        beforeShowDay: $.datepicker.noWeekends,
        beforeShowDay: DisableDates
    });

})
    </script>

CodePudding user response:

This will work:

var dates = ["06-08-2021"];

function DisableDates(date) {
  var noWeekend = $.datepicker.noWeekends(date);
  if (noWeekend[0]) {
    var string = jQuery.datepicker.formatDate('mm-dd-yy', date);
    return [dates.indexOf(string) == -1];
  } else {
    return noWeekend;
  }
}

$("#datepicker").datepicker({
  dateFormat: 'mm-dd-yy',
  beforeShowDay: DisableDates
});
<link href="http://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<p>Date: <input type="text" id="datepicker"></p>

  • Related