I have a pair of datepicker elements that I want to function as a range. When a value is set in the "low" datepicker, it should adjust the minDate and yearRange in the other datepicker, and vice versa with the "high" datepicker. The challenge I'm facing is handling the varied data types that I receive, such as dates, strings, numbers, or unknown inputs. This diversity makes it difficult for me to use the values effectively in setting the properties of the other datepickers. Below is an example of my code:
$(document).ready(function() {
$( '#from' ).datepicker({
showOn: 'button',
changeMonth: true,
changeYear: true,
defaultDate: null,
minDate: new Date(2009,0,1),
maxDate: null,
buttonImageOnly: true,
buttonImage: '/img/calendar_icon_20x20.png',
buttonText: 'select date from calendar',
dateFormat: 'dd-M-yy',
beforeShow:
function ( input, instance ) {
var value;
var options = {};
var minYear = new Date(instance.settings.minDate).getFullYear();
var maxYear = new Date(instance.settings.maxDate).getFullYear();
value = $( '#thru' ).val();
if (value == '') {
value = $( '#thru' ).datepicker('option', 'maxDate');
}
if (typeof value == 'object') {
options.maxDate = value;
} else {
options.maxDate = $.datepicker.parseDate('dd-M-yy', value.trim());
}
maxYear = options.maxDate.getFullYear();
options.yearRange = minYear + ':' + maxYear;
return options;
}
});
});
It seems like I may need to include an
if (typeof value == 'number') {..}
section to calculate a date based on a numerical days offset. Additionally, there is concern about handling string inputs that don't match the specified dateFormat and could cause parsing issues. I believe that the datepicker library likely has a method for standardizing input values into Date objects internally. Is there a way for me to access this standardized value directly, rather than attempting to handle it manually?