Alternatively, rather than listening to $('#[start/end datepicker id]').change
you can use the provided .on("change", function()
.
Fiddle or snippet below:
var interval = 7;
$(function() {
$("#sdatepicker").datepicker({
minDate: 0,
maxDate: interval
});
$("#edatepicker").datepicker({
minDate: 0
});
var today = new Date();
var target = new Date(today.getFullYear(), today.getMonth(), today.getDate() + interval);
$("#sdatepicker").datepicker("setDate", today);
$("#edatepicker").datepicker("setDate", target);
$("#sdatepicker").datepicker({
changeMonth: true,
changeYear: true,
showOn: 'button',
buttonImage: '../../../images/calendar.png',
buttonImageOnly: true,
title: 'Click to open calendar',
alt: 'Click to open calendar'
}).on("change", function() {
var start = $("#sdatepicker").datepicker("getDate");
start.setDate(start.getDate() + interval);
$("#edatepicker").datepicker("setDate", start);
$("#edatepicker").datepicker("option", "minDate",
$("#sdatepicker").datepicker("getDate"));
});
$("#edatepicker").datepicker({
changeMonth: true,
changeYear: true,
showOn: 'button',
buttonImage: '../../../images/calendar.png',
buttonImageOnly: true,
title: 'Click to open calendar',
alt: 'Click to open calendar'
}).on("change", function() {
var start = $("#sdatepicker").datepicker("getDate");
$("#sdatepicker").datepicker("setDate", start);
$("#sdatepicker").datepicker("option", "maxDate",
$("#edatepicker").datepicker("getDate"));
});
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<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>
<p>Start Date: <input type="text" id="sdatepicker"></p>
<p>End Date: <input type="text" id="edatepicker"></p>
Explanation:
minDate
is used to prevent past date to be selected. 0 is today.
Using interval you can update the start/end date with a fixed interval when any of them was changed, and to limit end date cannot be less than the start date
and start date cannot be greater than the end date you can combine maxDate
and minDate
:
$("#edatepicker").datepicker("option", "minDate", $("#sdatepicker").datepicker("getDate"));
and:
$("#sdatepicker").datepicker("option", "maxDate", $("#edatepicker").datepicker("getDate"));
Update
The following fiddle solved OP problem apparently as discussed. Snippet:
var interval = 7;
$(function() {
var start;
var today = new Date();
var target = new Date(today.getFullYear(), today.getMonth(), today.getDate() + interval);
$(".txtStartDate").datepicker("setDate", today);
$(".txtEndDate").datepicker("setDate", target);
$(".txtStartDate").datepicker({
minDate: 0,
changeMonth: true,
changeYear: true,
showOn: 'button',
buttonImage: '../../../images/calendar.png',
buttonImageOnly: true,
title: 'Click to open calendar',
alt: 'Click to open calendar',
onSelect: function() {
start = $(this).datepicker("getDate");
start.setDate(start.getDate() + interval);
$(".txtEndDate").datepicker("setDate", start);
$(".txtEndDate").datepicker("option", "minDate",
$(this).datepicker("getDate"));
$(this).change();
}
}).on("change", function() {
$(".txtEndDate").datepicker("setDate", start);
$(".txtEndDate").datepicker("option", "minDate",
$(this).datepicker("getDate"));
});
$(".txtEndDate").datepicker({
minDate: 0,
changeMonth: true,
changeYear: true,
showOn: 'button',
buttonImage: '../../../images/calendar.png',
buttonImageOnly: true,
title: 'Click to open calendar',
alt: 'Click to open calendar'
});
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<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>
<p>Start Date: <input type="text" class="txtStartDate"></p>
<p>End Date: <input type="text" class="txtEndDate"></p>
$(".txtEndDate").datepicker("option","minDate", start.getDate())
won't work because
var start = $(".txtStartDate").datepicker("getDate");
is initialized, then updated into start.setDate(start.getDate() + interval);
.
Which mean start.getDate()
won't result in $(".txtStartDate").datepicker("getDate");
While $(".txtEndDate").datepicker("option", "minDate",
$(".txtStartDate").datepicker("getDate"));
work.
Also, apparently previous month won't trigger onchange
or onselect
setting the end date while calling them twice and early initializing of var start;
would:
onSelect: function() {
start = $(this).datepicker("getDate");
start.setDate(start.getDate() + interval);
$(".txtEndDate").datepicker("setDate", start);
$(".txtEndDate").datepicker("option", "minDate",
$(this).datepicker("getDate"));
$(this).change();
}
}).on("change", function() {
$(".txtEndDate").datepicker("setDate", start);
$(".txtEndDate").datepicker("option", "minDate",
$(this).datepicker("getDate"));
});