Skip to content

Commit d75ff89

Browse files
committed
Fix date selection issue in createButton function
1 parent b69aa7e commit d75ff89

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

script.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ const displayDates = () => {
9595

9696
for (let i = 0; i <= lastOfPrevMonth.getDay(); i++) {
9797
const text = lastOfPrevMonth.getDate() - lastOfPrevMonth.getDay() + i;
98-
const button = createButton(text, true);
98+
const button = createButton(text, true, -1);
9999
dates.appendChild(button);
100100
}
101101

@@ -117,25 +117,25 @@ const displayDates = () => {
117117
for (let i = firstOfNextMonth.getDay(); i < 7; i++) {
118118
const text = firstOfNextMonth.getDate() - firstOfNextMonth.getDay() + i;
119119

120-
const button = createButton(text, true);
120+
const button = createButton(text, true, 1);
121121
dates.appendChild(button);
122122
}
123123
};
124124

125-
const createButton = (text, isDisabled = false) => {
125+
const createButton = (text, isDisabled = false, type = 0) => {
126126
const currentDate = new Date();
127127

128+
// determine the date to compare based on the button type
129+
let comparisonDate = new Date(year, month + type, text);
130+
128131
// check if the current button is the date today
129132
const isToday =
130133
currentDate.getDate() === text &&
131134
currentDate.getFullYear() === year &&
132135
currentDate.getMonth() === month;
133136

134137
// check if the current button is selected
135-
const selected =
136-
selectedDate.getDate() === text &&
137-
selectedDate.getFullYear() === year &&
138-
selectedDate.getMonth() === month;
138+
const selected = selectedDate.getTime() === comparisonDate.getTime();
139139

140140
const button = document.createElement("button");
141141
button.textContent = text;

0 commit comments

Comments
 (0)