Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to disable already booked dates?

I am having a form for booking hotel rooms where in I am having two fields called checkIn and checkOut. I am using jQuery datepicker for booking rooms here I don't want to show those dates which are already booked. I have tried like this.

$(function() {
  var excludedCheckInDates = CHECKINDATES; // an array of already booked checkin dates           
  var excludedCheckOutDates = CHECKOUTDATES; // an array of already booked checkout dates
  $.datepicker
    .setDefaults({
      defaultDate: '+1w',
      changeMonth: true,
      changeYear: true,
      minDate: 0,
      beforeShowDay: function(date) {
        date = $.datepicker.formatDate('yy-mm-dd', date);
        excludedCheckInDates = $.inArray(date,
          excludedCheckInDates) < 0;
        excludedCheckOutDates = $.inArray(date,
          excludedCheckOutDates) < 0;
        if (excludedCheckInDates) {
          return [true, 'selectedDate'];
        } else {
          return false;
        }
        if (excludedCheckOutDates) {
          return true;
        } else {
          return false;
        }
        return true;
      }
    });
  $('#checkIn').datepicker({
    onSelect: function(selectedDate) {
      $('#checkIn').datepicker('option', 'minDate',
        selectedDate || 0);
    }
  });
  $('#checkOut').datepicker({
    onSelect: function(selectedDate) {
      $('#checkOut').datepicker('option', 'maxDate', selectedDate);
    }
  });
});
like image 361
Suleman khan Avatar asked May 24 '15 16:05

Suleman khan


People also ask

How to disable today date in datepicker?

To disable dates through using a function, set the return value for the date that will be disabled to true .

How to disable days in jQuery datepicker?

Disable Weekends $(function() { $( "#availability" ). datepicker({ minDate: 0, dateFormat: 'mm/dd/yy', inline: true, numberOfMonths: [1, 2], dayNamesMin: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], beforeShowDay: $.


1 Answers

This fiddle should help you, you just need to figure out the array of dates you want to disable

 var array = ["2015-06-14","2015-06-15","2015-06-16"]

$('input').datepicker({
  beforeShowDay: function(date){
    var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
    return [ array.indexOf(string) == -1 ]
  }
});

http://jsfiddle.net/CxNNh/2201/

here is the updated jsfiddle that works for me

http://jsfiddle.net/CxNNh/2202/

like image 141
VladNeacsu Avatar answered Oct 01 '22 23:10

VladNeacsu