Fix days of month depending on particular year:
There are some cases where we have two dropdowns(for month and days) and upon selecting month days dropdown needs to be update so i wrote a few lins of code for it and hope it helps anyone face this situation
This is the method which actually returns the number of days available for a month and year:
correctDaysInMonth = 32 - new Date(year, month, 32).getDate();
function which adjust the nmber of days dropdown upon selecting year dropdown :
function AdjustDaysOfTheMonth(controlID, month, year) {
//Calculate exact number of day's in a month
//when we pass the parameter days greater than the particular month available days
// It throws the actual number of days available
correctDaysInMonth = 32 - new Date(year, month, 32).getDate();
console.log('exact Dates : ' + controlID + ' : ' + +correctDaysInMonth);
var maxDayValue = Number($(controlID + " option:last-child").val());
if (maxDayValue != correctDaysInMonth) {
// If maximum value of the days dropdown is less than the
// actual days add additional days
if (maxDayValue < correctDaysInMonth) {
//Add element from the range of month
for (var count = maxDayValue + 1; count <= correctDaysInMonth; count++) {
//Create option element
var dayElementOption = document.createElement('option');
dayElementOption.text = count;
dayElementOption.value = count;
//Add it to control
try {
$(controlID)[0].add(dayElementOption, null); // Fail in IE
}
catch (ex) {
$(controlID)[0].add(dayElementOption); //work in IE only
}
}
}
else {
//Remove element not in the range of month
for (var count = maxDayValue + 1; count > correctDaysInMonth; count--) {
$(controlID)[0].remove(count);
}
}
}
}