Skip to content

Commit d4c1a7c

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

File tree

1 file changed

+3
-15
lines changed

1 file changed

+3
-15
lines changed

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,9 @@ 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-
// Helper function to check if the given year is a leap year
22-
const isLeapYear = year => new Date(year, 1, 29).getMonth() === 1;
23-
24-
// Determine the maximum days for the selected month
25-
let maxDay;
26-
if (month === 2) {
27-
// February
28-
maxDay = isLeapYear(year) ? 29 : 28;
29-
} else if ([4, 6, 9, 11].includes(month)) {
30-
// April, June, September, November
31-
maxDay = 30;
32-
} else {
33-
// All other months
34-
maxDay = 31;
35-
}
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();
3624

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

0 commit comments

Comments
 (0)