Godric Godric - 1 month ago 7
Javascript Question

Datepicker range is not working

I am trying to create a date range where after selecting

start_date
the
end_date
will only allow the user to choose dates after the start_date and vice versa. Here is a code snippet:

main.js
function set_parameters() {
var date_start_input=$('#start_date');
var date_end_input=$('#end_date');
var container=$('.bootstrap-iso form').length>0 ? $('.bootstrap-iso form').parent() : "body";
var options_start={
format: 'MM dd, yyyy',
container: container,
todayHighlight: true,
autoclose: true,
orientation: 'top left',
onSelect: function(selectedDate) {
date_end_input.datepicker("option", "maxDate", selectedDate);
}
};
var options_end={
format: 'MM dd, yyyy',
container: container,
todayHighlight: true,
autoclose: true,
orientation: 'top left',
onSelect: function(selectedDate) {
date_start_input.datepicker("option", "minDate", selectedDate);
}
};
date_start_input.datepicker(options_start);
date_end_input.datepicker(options_end);
};

$(document).ready(function() {
set_parameters();
});

index.html
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<link rel="stylesheet" href="https://formden.com/static/cdn/bootstrap-iso.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/js/bootstrap-datepicker.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/css/bootstrap-datepicker3.css"/>
</head>
...
<input class="form-control" id="start_date" name="start_date" placeholder="MM DD, YYYY" type="text" style="border-radius: 0;"/>
<input class="form-control" id="end_date" name="end_date" placeholder="MM DD, YYYY" type="text" style="border-radius: 0;"/>
...


I tried solutions from a couple of topics yet none of them worked for me. I believe that the most important things are to set max and min date when the change is made in dates.

Answer

There is a couple of errors in your code:

  • the datepicker has no option onSelect, instead you have to listen for changeDate event
  • the datepicker has neither minDate nor maxDate option/method, you have to use setStartDate and setEndDate

Here a working example:

function set_parameters() {
  var date_start_input = $('#start_date');
  var date_end_input = $('#end_date');
  var container = $('.bootstrap-iso form').length>0 ? $('.bootstrap-iso form').parent() : "body";
   
  var options = {
    format: 'MM dd, yyyy',
    container: container,
    todayHighlight: true,
    autoclose: true,
    orientation: 'top left'
  };

  date_start_input.datepicker(options).on('changeDate', function(e){
    date_end_input.datepicker("setStartDate", e.date);
  });
  date_end_input.datepicker(options).on('changeDate', function(e){
    date_start_input.datepicker("setEndDate", e.date);
  });
};

$(document).ready(function() {
   set_parameters();
});
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/js/bootstrap-datepicker.min.js"></script>

<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.css" rel="stylesheet"/>
<link rel="stylesheet" href="https://formden.com/static/cdn/bootstrap-iso.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/css/bootstrap-datepicker3.css"/>

<input class="form-control" id="start_date" name="start_date" placeholder="MM DD, YYYY" type="text" style="border-radius: 0;"/>
<input class="form-control" id="end_date" name="end_date" placeholder="MM DD, YYYY" type="text" style="border-radius: 0;"/>