I followed the example in this post:jquery datetime picker set minDate dynamic.
I tried 2 ways:
First method
this method only works for FIRST TIME selecting "to" date. That's to say, after selecting once "from" and "to" date, I come back to re-select the "from“ date, then "to" date drop down list doesn't change accordingly, it remains as the 1st time I chose:
$('.to').datepicker({
beforeShow: function(input, inst) {
var mindate = $('.from').datepicker('getDate');
$(this).datepicker('option', 'minDate', mindate);
}
});
html: to select from "from" date calendar
<script type="text/javascript">
$(function() {
$('.from').datepicker(
{
dateFormat: "yy/mm",
changeMonth: true,
changeYear: true,
showButtonPanel: true,
onClose: function(dateText, inst) {
function isDonePressed(){
return ($('#ui-datepicker-div').html().indexOf('ui-datepicker-close ui-state-default ui-priority-primary ui-corner-all ui-state-hover') > -1);
}
if (isDonePressed()){
var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
$(this).datepicker('setDate', new Date(year, month, 1)).trigger('change');
$('.date-picker').focusout()//Added to remove focus from datepicker input box on selecting date
}
},
beforeShow : function(input, inst) {
inst.dpDiv.addClass('month_year_datepicker')
if ((datestr = $(this).val()).length > 0) {
year = datestr.substring(datestr.length-4, datestr.length);
month = datestr.substring(0, 2);
$(this).datepicker('option', 'defaultDate', new Date(year, month-1, 1));
$(this).datepicker('setDate', new Date(year, month-1, 1));
$(".ui-datepicker-calendar").hide();
}
}
})
});
</script>
I put in the https://jsfiddle.net/3w3h097c/ . In the fiddle, the drop down calendar doesn't appear I don't know why, but it indeed appears in my browser.
to select from "to" date calendar
The only different compared to select from "from" date calendar is to add the following 2 sentences:
beforeShow : function(input, inst) {
var mindate = $('.from').datepicker('getDate'); // Added sentence, the rest same
$(this).datepicker('option', 'minDate', mindate); //>Added sentence,the rest same
inst.dpDiv.addClass('month_year_datepicker')
if ((datestr = $(this).val()).length > 0) {
year = datestr.substring(datestr.length-4, datestr.length);
month = datestr.substring(0, 2);
$(this).datepicker('option', 'defaultDate', new Date(year, month-1, 1));
......
Second method--doesn't work at all
Add a "onSelect: function(selected)” for both "from " and "to".
<---from--->
$(function() {
$('.from').datepicker(
{
dateFormat: "yy/mm",
changeMonth: true,
changeYear: true,
showButtonPanel: true,
<!--add onSelect here---->
onSelect: function(selected) {
$(".to").datepicker("option","minDate", selected)
},
onClose: function(dateText, inst) {
function isDonePressed(){
return ($('#ui-datepicker-div').html().indexOf('ui-datepicker-close ui-state-default ui-priority-primary ui-corner-all ui-state-hover') > -1);
}
if (isDonePressed()){
var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
$(this).datepicker('setDate', new Date(year, month, 1)).trigger('change');
$('.from').focusout()//Added to remove focus from datepicker input box on selecting date
}
},
beforeShow : function(input, inst) {
inst.dpDiv.addClass('month_year_datepicker')
if ((datestr = $(this).val()).length > 0) {
year = datestr.substring(datestr.length-4, datestr.length);
month = datestr.substring(0, 2);
$(this).datepicker('option', 'defaultDate', new Date(year, month-1, 1));
$(this).datepicker('setDate', new Date(year, month-1, 1));
$(".ui-datepicker-calendar").hide();
}
}
})
});
<!--to:-->
$(function() {
$('.to').datepicker(
........
onSelect: function(selected) {
$('.from').datepicker("option","maxDate", selected)
},
.......