Skip to content

Commit

Permalink
gw-preserve-datepicker-date-selection-when-changing-months.js: Fixe…
Browse files Browse the repository at this point in the history
…d an issue with edge cases like 29 February, 31 April etc.
  • Loading branch information
saifsultanc committed Nov 5, 2024
1 parent 684f516 commit d4c1a7c
Showing 1 changed file with 3 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,9 @@ gform.addFilter( 'gform_datepicker_options_pre_init', function( optionsObj, form
var selectedDate = $(this).datepicker('getDate');
var day = selectedDate ? selectedDate.getDate() : 1;

// Helper function to check if the given year is a leap year
const isLeapYear = year => new Date(year, 1, 29).getMonth() === 1;

// Determine the maximum days for the selected month
let maxDay;
if (month === 2) {
// February
maxDay = isLeapYear(year) ? 29 : 28;
} else if ([4, 6, 9, 11].includes(month)) {
// April, June, September, November
maxDay = 30;
} else {
// All other months
maxDay = 31;
}
// Create a date with the first of the selected month and year
var newDate = new Date(year, month, 0); // Last day of the previous month (i.e., correct for leap years)
var maxDay = newDate.getDate();

// Adjust the day if it exceeds the maximum days in the selected month
day = Math.min(day, maxDay);
Expand Down

0 comments on commit d4c1a7c

Please sign in to comment.