Reputation: 11
I am trying to make a webpage which has a datepicker, but i can not figure out how i can set the min and max date dynamically.I am trying to achieve is
$(document).ready(function() {
$(function() {
$('#txtArrivalDate').datepicker({
dateFormat: 'mm-dd-yy',
minDate: "-0d",
onSelect: function(date) {
selectedDate = date;
$('#txtDepartureDate').datepicker('option', 'minDate', date);
$("#txtDepartureDate").datepicker("refresh");
$('#txtDepartureDate').datepicker('option', 'firstDay', date);
$('#txtDepartureDate').datepicker('option', 'maxDate', '+7d');
}, //end of onSelect attribute
onClose: function(date) {
}, //end of onClose
}); //end of txtArrivalDate datepicker
}); //end of txtArrivalDate function
$("#txtArrivalDate").change(function() {
$('#txtDepartureDate').datepicker('option', 'firstDay', selectedDate);
$('#txtDepartureDate').datepicker('option', 'maxDate', '+7d');
});
$(function() {
$('#txtDepartureDate').datepicker({
dateFormat: 'mm-dd-yy'
});
}) //end of txtDepartureDate function
}); //end of Ready function
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
Arrival Date:-
<input type="text" id="txtArrivalDate" /> <br /><br />
Departure Date:-
<input type="text" id="txtDepartureDate" /> <br /><br />
Upvotes: 1
Views: 5934
Reputation: 1302
Try this code
$(document).ready(function(){
$('#txtArrivalDate').datepicker({
dateFormat: 'mm-dd-yy',
minDate : "-0d",
});
$('#txtArrivalDate').change(function(){
var sDate = $(this).datepicker("getDate");
var minDate = $(this).datepicker("getDate");
sDate.setDate(sDate.getDate()+7);
$('#txtDepartureDate').datepicker({
dateFormat: 'mm-dd-yy',
maxDate : sDate,
minDate : minDate,
});
})
});
Good Luck..
Upvotes: 2