-2

I need to disable multiple dates for a single month in a jQuery Datepicker. Here is my code:

<head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script> </head> <body> <div class="input-group date"> <div class="input-group-addon"> <i class="fa fa-calendar"></i></div> <input value="" readonly="" class="form-control" name="doi" id="doi" type="text"> </div> <script type='text/javascript'> var array = ["09/14/2019","09/15/2019","09/16/2019"]; $('#doi').datepicker({ beforeShowDay: function(date){ var string = jQuery.datepicker.formatDate('mm/dd/yy', date); return [ array.indexOf(string) == -1 ] }, daysOfWeekDisabled: [0], //Disable sunday autoclose:true, }) ; </script> </body>

But disabling the dates is not working.

Expected: I want to disable these dates: 09/14/2019,09/15/2019,09/16/2019 in the datepicker.

1
  • Based on your code, this works fine. Commented Mar 21 at 20:17

3 Answers 3

5

you need to add jquery-ui.min.js and jquery-ui.css

var array = ["2019-09-14","2019-09-15","2019-09-16"] $('#doi').datepicker({ beforeShowDay: function(date){ var string = jQuery.datepicker.formatDate('yy-mm-dd', date); return [ array.indexOf(string) == -1 ] } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/themes/vader/jquery-ui.css" rel="stylesheet"/> <input id="doi" />

Sign up to request clarification or add additional context in comments.

5 Comments

@Ninjin Koderi Thanks for your response. But this also not working. As i'm using bootstrap-datepicker.min.js for styling and other features also. After Including all your cdn and scripting stuff my datepicker becomes constant (neither putting any error nor disabling date).
Then why did you put above as jquery datepicker??
because i'm using : <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
That is for jQuery right? not for the datepicker.
A .css file is not required for programmatic logic to work. OP already includes jQuery UI in their scripts, so your prose is completely incorrect here. In fact, this code doesn't seem to change anything except the format of the date in the datepicker logic.
0

Try below solution for Bootstrap Datepicker. dont be confuse with both. from jQuery.datepicker.formatDate('mm/dd/yy', date) its clear that you are trying to use Jquery Datepicker.

var datesForDisable = ["2019-09-14","2019-09-15","2019-09-16"] $("#datepicker").datepicker({ format: 'dd/mm/yyyy', autoclose: true, weekStart: 1, calendarWeeks: true, todayHighlight: true, beforeShowDay: function (currentDate) { var dayNr = currentDate.getDay(); var dateNr = moment(currentDate.getDate()).format("DD-MM-YYYY"); if (datesForDisable.length > 0) { for (var i = 0; i < datesForDisable.length; i++) { if (moment(currentDate).unix()==moment(datesForDisable[i],'YYYY-MM-DD').unix()){ return false; } } } return true; } })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script> <input id="datepicker">

Comments

0

Here was my solution:

var datearray = ["09/14/2019","09/15/2019","09/16/2019"]; $('#doi').datepicker({ format: 'mm/dd/yyyy', datesDisabled: datearray, daysOfWeekDisabled: [0], //Disable sunday autoclose:true, todayHighlight: true, }); 

Here, datesDisabled: plays vital role for me.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.