datePicker Date Range with maxDate 1 month
/**
* datePicker Date Range
* From: today
* To: 1 month
*
* Update to 1 month when the DateFrom is changed too.
*
* Original Code from: https://jqueryui.com/datepicker/#date-range
*/
var dateFormat = "dd/mm/yy";
// Date From - This should start from today
var from = jQuery("#from").datepicker({
dateFormat: dateFormat,
minDate: 0,
numberOfMonths: 2,
}).on('change', function(e) {
var thisMinDate = getDate( this ),
oneMonthAhead = new Date( getDate( this ) );
if ( oneMonthAhead ) {
oneMonthAhead.setMonth( oneMonthAhead.getMonth() + 1 );
}
to.datepicker( "option", "minDate", thisMinDate );
to.datepicker( "option", "maxDate", new Date( oneMonthAhead ) );
});
// Date To - This should have 1 month ahead of 'from' date.
var to = jQuery("#to").datepicker({
minDate: 0,
maxDate: '+1m',
numberOfMonths: 2,
}).on( "change", function() {
from.datepicker( "option", "maxDate", getDate( this ) );
});
function getDate( element ) {
var date;
try {
date = $.datepicker.parseDate( dateFormat, element.value );
} catch( error ) {
date = null;
}
return date;
}