Skip to content

Commit 70187c6

Browse files
committed
Fix a minor issue with the datepicker.
With the current code if you are in a month that does not have a day (such as April that does not have the 31st), one of those days is selected in another month that does have that day, and you click the "Today" button, then it doesn't go to today. Instead it goes to the date of today in the next month. This is because the code sets the year, month, and day for today on the selected date object and in that order, and uses the modified selected date to set the flatpickr date. So for example, if today is April 7, 2026, and the selected date is July 31, 2026, then the selected date's year is set to 2026, then the month set to April. So at that point the selected date is April 31, 2026, which really becomes May 1, 2026. Then the day is set, and so you get May 7, 2026. This fixes the issue by starting with todays date at 12:00 am, and then setting the time of todays date to the time of the selected date, and then using that to set the flatpickr date.
1 parent fb3b720 commit 70187c6

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

htdocs/js/DatePicker/datepicker.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,14 @@
119119
],
120120
onClick: (index, fp) => {
121121
if (index === 0) {
122-
const today = new Date();
123-
// If there isn't a selected date, then use 12:00 am on the current date.
124-
const selectedDate = fp.selectedDates[0] ?? new Date(new Date().toDateString());
125-
selectedDate.setFullYear(today.getFullYear());
126-
selectedDate.setMonth(today.getMonth());
127-
selectedDate.setDate(today.getDate());
128-
fp.setDate(selectedDate, true);
122+
// The initial date represents 12:00 am on the current date.
123+
const today = new Date(new Date().toDateString());
124+
if (fp.selectedDates[0]) {
125+
today.setHours(fp.selectedDates[0].getHours());
126+
today.setMinutes(fp.selectedDates[0].getMinutes());
127+
today.setSeconds(fp.selectedDates[0].getSeconds());
128+
}
129+
fp.setDate(today, true);
129130
} else if (index === 1) {
130131
fp.setDate(new Date(), true);
131132
}

htdocs/js/ProblemSetList/problemsetlist.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,14 @@
251251
],
252252
onClick: (index, fp) => {
253253
if (index === 0) {
254-
const today = new Date();
255-
// If there isn't a selected date, then use 12:00 am on the current date.
256-
const selectedDate = fp.selectedDates[0] ?? new Date(new Date().toDateString());
257-
selectedDate.setFullYear(today.getFullYear());
258-
selectedDate.setMonth(today.getMonth());
259-
selectedDate.setDate(today.getDate());
260-
fp.setDate(selectedDate);
254+
// The initial date represents 12:00 am on the current date.
255+
const today = new Date(new Date().toDateString());
256+
if (fp.selectedDates[0]) {
257+
today.setHours(fp.selectedDates[0].getHours());
258+
today.setMinutes(fp.selectedDates[0].getMinutes());
259+
today.setSeconds(fp.selectedDates[0].getSeconds());
260+
}
261+
fp.setDate(today, true);
261262
} else if (index === 1) {
262263
fp.setDate(new Date());
263264
}

0 commit comments

Comments
 (0)