title: Jquery - Datepicker Calculating the days between two dates
body: <p>I am using jquery datepicker and trying to find the days between two dates. However, some dates are not selectable according to my database, so I have some unavailable dates. </p>
  
  <p>What I would like to do is to check the selected date range if they are all available (otherwise give error with div) and then calculate the number of days. </p>
  
  <p>I have come this far;
  What I can do is to disable the days on the calendar according to my database if they are booked. But I am not able to get the value(dates from input) outside scope of the code so that I can check if there is any booked day between the date range. </p>
  
  <pre><code>&lt;div class="row"&gt;
      &lt;!-- Date Picker--&gt;
      &lt;label for="from"&gt;From&lt;/label&gt;
      &lt;input type="text" id="from" name="from"&gt;
      &lt;label for="to"&gt;to&lt;/label&gt;
      &lt;input type="text" id="to" name="to"&gt;
      &lt;!-- /.Date Picker --&gt;
      &lt;div id="combined-dates" style="display:none;"&gt;Please select unbooked range..&lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;script&gt;
      $(function() {
          $.ajax({
              type: "GET",
              dataType: "json",
              url: "/houses/&lt;%= @house.id %&gt;",
              success: function(schedules) {
  
                  var length = Object.keys(schedules).length
  
  
                  var array = []
                  for (var i = 0; i &lt; length; i++) {
  
                      if (schedules[i].status = " booked ") {
                          array.push(schedules[i].date)
  
                      }
  
                  }
  
  
                  $("#from").datepicker({
                      defaultDate: "+1w",
                      changeMonth: true,
                      numberOfMonths: 1,
                      dateFormat: "dd/mm/yy",
                      onClose: function(selectedDate) {
                          $("#to").datepicker("option", "minDate", selectedDate);
                          //var selected1 = selectedDate.split("/")
                          //console.log(selected1)
                      },
                      beforeShowDay: function(date) {
                          var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
                          return [array.indexOf(string) == -1]
                      }
                  });
                  $("#to").datepicker({
                      defaultDate: "+1w",
                      changeMonth: true,
                      numberOfMonths: 1,
                      dateFormat: "dd/mm/yy",
                      onClose: function(selectedDate) {
                          $("#from").datepicker("option", "maxDate", selectedDate);
                          //var selected2 = selectedDate.split("/")
                          //console.log(selected2)
                      },
                      beforeShowDay: function(date) {
                          var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
                          return [array.indexOf(string) == -1]
  
                      }
                  });
                  //Non of them works!!
                  //var d = $('#from').datepicker().val();
                  //var d = $('#from').datepicker('getDate').val();
                  //console.log(d);
              }
          });
  
  
      });
  &lt;/script&gt;
  </code></pre>