diff --git a/src/components/Calendar.tsx b/src/components/Calendar.tsx index 4de0509..a3cd556 100644 --- a/src/components/Calendar.tsx +++ b/src/components/Calendar.tsx @@ -145,7 +145,9 @@ export const Calendar: React.FC = ({ e.stopPropagation(); await onToggleHabit(habitId, date); - // Update the selected date data immediately after toggling + // Ensure we're using the correct date for the selected date display + const selectedUTCDate = new Date(date + 'T00:00:00.000Z'); + setSelectedDate({ date, completedHabits: getCompletedHabitsForDate(date), @@ -157,6 +159,16 @@ export const Calendar: React.FC = ({ }); }; + // Update where dates are displayed + const formatDisplayDate = (dateStr: string) => { + const date = new Date(dateStr + 'T00:00:00.000Z'); + return date.toLocaleDateString('default', { + month: 'long', + day: 'numeric', + year: 'numeric' + }); + }; + return ( <>
= ({ `}>

- {new Date(selectedDate.date).toLocaleDateString('default', { - month: 'long', - day: 'numeric', - year: 'numeric' - })} + {formatDisplayDate(selectedDate.date)}

diff --git a/src/components/HabitList.tsx b/src/components/HabitList.tsx index 8c9c4d4..00fd50e 100644 --- a/src/components/HabitList.tsx +++ b/src/components/HabitList.tsx @@ -32,7 +32,7 @@ export function HabitList({ }, [habits, habitSort]); const getDayName = (dateStr: string) => { - const date = new Date(dateStr); + const date = new Date(dateStr + 'T00:00:00.000Z'); return daysOfWeek[getDayIndex(date)]; }; @@ -47,7 +47,7 @@ export function HabitList({
{getDayName(dateStr)}
{getDayName(dateStr).slice(0, 1)}
- {new Date(dateStr).getDate()} + {new Date(dateStr + 'T00:00:00.000Z').getUTCDate()}
))} diff --git a/src/hooks/useHabits.ts b/src/hooks/useHabits.ts index 27e6b7a..bbb04b4 100644 --- a/src/hooks/useHabits.ts +++ b/src/hooks/useHabits.ts @@ -139,7 +139,6 @@ export const useHabits = () => { const isCompleted = habit.completedDates.includes(date); if (isCompleted) { - // Remove completion const { error } = await supabase .from('habit_completions') .delete() @@ -149,7 +148,6 @@ export const useHabits = () => { if (error) throw error; } else { - // Add completion const { error } = await supabase .from('habit_completions') .insert({ @@ -177,7 +175,6 @@ export const useHabits = () => { } catch (err) { console.error('Error toggling habit:', err); setError(err instanceof Error ? err.message : 'Failed to toggle habit'); - // Refresh habits to ensure consistency await fetchHabits(); } }; diff --git a/src/utils/dateUtils.ts b/src/utils/dateUtils.ts index 5b980f2..f58cce6 100644 --- a/src/utils/dateUtils.ts +++ b/src/utils/dateUtils.ts @@ -1,10 +1,13 @@ // Simple date formatting without timezone complications export const formatDate = (date: Date): string => { - const year = date.getFullYear(); - const month = String(date.getMonth() + 1).padStart(2, '0'); - const day = String(date.getDate()).padStart(2, '0'); - return `${year}-${month}-${day}`; - }; + // Create a new date object at midnight UTC + const d = new Date(Date.UTC( + date.getFullYear(), + date.getMonth(), + date.getDate() + )); + return d.toISOString().split('T')[0]; +}; // Get day index (0-6) where Monday is 0 and Sunday is 6 export const getDayIndex = (date: Date): number => { @@ -14,17 +17,22 @@ export const formatDate = (date: Date): string => { // Get dates for current week starting from Monday export const getWeekDates = (baseDate: Date = new Date()): string[] => { - const current = new Date(baseDate); + // Create date at midnight UTC + const current = new Date(Date.UTC( + baseDate.getFullYear(), + baseDate.getMonth(), + baseDate.getDate() + )); const dayIndex = getDayIndex(current); // Adjust to Monday - current.setDate(current.getDate() - dayIndex); + current.setUTCDate(current.getUTCDate() - dayIndex); // Generate dates starting from Monday const dates = []; for (let i = 0; i < 7; i++) { const date = new Date(current); - date.setDate(current.getDate() + i); + date.setUTCDate(current.getUTCDate() + i); dates.push(formatDate(date)); } @@ -45,3 +53,12 @@ export const formatDate = (date: Date): string => { date1.getFullYear() === date2.getFullYear() ); }; + + // Add helper function to convert local date to UTC midnight + export const getUTCMidnight = (date: Date): Date => { + return new Date(Date.UTC( + date.getFullYear(), + date.getMonth(), + date.getDate() + )); + };