Last active
February 15, 2017 08:20
-
-
Save zeshanshani/8086618fc15e6dec28c21013fd97f664 to your computer and use it in GitHub Desktop.
datePicker Date Range with maxDate 1 month
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment