Skip to content

Commit

Permalink
Fix incorrect TimeTable's day of week calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Denys Stetsenko committed Dec 4, 2023
1 parent e3cdb99 commit 61d5369
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
7 changes: 6 additions & 1 deletion app/components/TimePickerTable/TimePickerTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ const TimePickerTable: React.FC<TimePickerTableProps> = ({
);

useEffect(() => {
if (!timeSlots[start] || !timeSlots[end]) {
if (
!timeSlots ||
timeSlots.length === 0 ||
!timeSlots[start] ||
!timeSlots[end]
) {
return;
}

Expand Down
33 changes: 20 additions & 13 deletions app/utils/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,33 @@ export const getDayOfWeek = (date: Date = new Date()) => {
return date.toLocaleDateString("en-US", { weekday: "long" });
};

export const daysIntoYear = (date: Date = new Date()) => {
return (
(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) -
Date.UTC(date.getFullYear(), 0, 0)) /
24 /
60 /
60 /
1000
);
};
// export const daysIntoYear = (date: Date = new Date()) => {
// return (
// (Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) -
// Date.UTC(date.getFullYear(), 0, 0)) /
// 24 /
// 60 /
// 60 /
// 1000
// );
// };

export const getDayOfWeekNumbered = (date: Date = new Date()) => {
if (START_FROM_MONDAY) {
return (date.getDay() - 1 + 7) % 7;
}

const xInYear = daysIntoYear(date);
const todayInYear = daysIntoYear(new Date());
const today = new Date();
const todayIndex = today.getDay();
const passedDateIndex = date.getDay();

let diff = passedDateIndex - todayIndex;

if (diff < 0) {
diff += 7;
}

return (xInYear - todayInYear) % 7;
return diff;
};

export const getWeekDates = (dateString: string): Date[] => {
Expand Down

0 comments on commit 61d5369

Please sign in to comment.