From 9b1ef646187838964e5d9e5f3c00f9288d0e049e Mon Sep 17 00:00:00 2001 From: Levente Orban Date: Mon, 20 Oct 2025 11:25:35 +0200 Subject: [PATCH] fix: creating an event and showing the wrong date --- src/lib/calendarHelpers.ts | 5 ++++- src/lib/dateHelpers.ts | 18 ++++++++++++------ src/routes/create/+page.server.ts | 10 ++++++++-- src/routes/discover/+page.svelte | 14 ++++++++++---- src/routes/event/[id]/edit/+page.server.ts | 5 +++-- 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/lib/calendarHelpers.ts b/src/lib/calendarHelpers.ts index efb38e7..925b880 100644 --- a/src/lib/calendarHelpers.ts +++ b/src/lib/calendarHelpers.ts @@ -15,7 +15,10 @@ export interface CalendarEvent { * Formats a date and time string for iCal format (UTC) */ export const formatDateForICal = (date: string, time: string): string => { - const eventDate = new Date(`${date}T${time}`); + // Parse date and time as local timezone to avoid timezone issues + const [year, month, day] = date.split('-').map(Number); + const [hours, minutes, seconds] = time.split(':').map(Number); + const eventDate = new Date(year, month - 1, day, hours, minutes, seconds || 0); return eventDate.toISOString().replace(/[-:]/g, '').split('.')[0] + 'Z'; }; diff --git a/src/lib/dateHelpers.ts b/src/lib/dateHelpers.ts index d9536c5..6abb16c 100644 --- a/src/lib/dateHelpers.ts +++ b/src/lib/dateHelpers.ts @@ -1,11 +1,14 @@ import type { Event } from './types'; export const formatDate = (dateString: string): string => { - const date = new Date(dateString); - 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}`; + // Parse the date string as local date to avoid timezone issues + // Split the date string and create a Date object in local timezone + const [year, month, day] = dateString.split('-').map(Number); + const date = new Date(year, month - 1, day); // month is 0-indexed in Date constructor + const formattedYear = date.getFullYear(); + const formattedMonth = String(date.getMonth() + 1).padStart(2, '0'); + const formattedDay = String(date.getDate()).padStart(2, '0'); + return `${formattedYear}/${formattedMonth}/${formattedDay}`; }; export const formatTime = (timeString: string): string => { @@ -17,7 +20,10 @@ export const formatTime = (timeString: string): string => { export const isEventInTimeRange = (event: Event, timeFilter: string): boolean => { if (timeFilter === 'any') return true; - const eventDate = new Date(`${event.date}T${event.time}`); + // Parse date and time as local timezone to avoid timezone issues + const [year, month, day] = event.date.split('-').map(Number); + const [hours, minutes, seconds] = event.time.split(':').map(Number); + const eventDate = new Date(year, month - 1, day, hours, minutes, seconds || 0); const now = new Date(); // Handle temporal status filters diff --git a/src/routes/create/+page.server.ts b/src/routes/create/+page.server.ts index d8d0d56..f34b14f 100644 --- a/src/routes/create/+page.server.ts +++ b/src/routes/create/+page.server.ts @@ -56,7 +56,13 @@ export const actions: Actions = { }); } - if (new Date(date) < new Date()) { + // Check if date is in the past using local timezone + const [year, month, day] = date.split('-').map(Number); + const eventDate = new Date(year, month - 1, day); + const today = new Date(); + today.setHours(0, 0, 0, 0); + + if (eventDate < today) { return fail(400, { error: 'Date cannot be in the past.', values: { @@ -105,7 +111,7 @@ export const actions: Actions = { type: type, attendeeLimit: type === 'limited' ? parseInt(attendeeLimit) : null, visibility: visibility, - userId: userId + userId: userId! }) .catch((error) => { console.error('Unexpected error', error); diff --git a/src/routes/discover/+page.svelte b/src/routes/discover/+page.svelte index 1381fb0..9c7c13b 100644 --- a/src/routes/discover/+page.svelte +++ b/src/routes/discover/+page.svelte @@ -1,7 +1,6 @@