Skip to content

Commit e85d0ef

Browse files
authored
gw-preserve-datepicker-date-selection-when-changing-months.js: Fixed an issue with edge cases like 29 February, 31 April etc.
1 parent 1106636 commit e85d0ef

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

gravity-forms/gw-preserve-datepicker-date-selection-when-changing-months.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,16 @@ gform.addFilter( 'gform_datepicker_options_pre_init', function( optionsObj, form
1818
var selectedDate = $(this).datepicker('getDate');
1919
var day = selectedDate ? selectedDate.getDate() : 1;
2020

21-
// Set the new date with the updated month and year
21+
// Create a date with the first of the selected month and year
22+
var newDate = new Date(year, month, 0); // Last day of the previous month (i.e., correct for leap years)
23+
var maxDay = newDate.getDate();
24+
25+
// Adjust the day if it exceeds the maximum days in the selected month
26+
day = Math.min(day, maxDay);
27+
28+
// Set the new date with the updated month, year, and adjusted day
2229
$(this).datepicker('setDate', new Date(year, month - 1, day));
2330
}
24-
25-
return optionsObj;
31+
32+
return optionsObj;
2633
} );

0 commit comments

Comments
 (0)