fix: creating an event and showing the wrong date

This commit is contained in:
Levente Orban
2025-10-20 11:25:35 +02:00
parent c340088434
commit 9b1ef64618
5 changed files with 37 additions and 15 deletions

View File

@@ -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);

View File

@@ -1,7 +1,6 @@
<script lang="ts">
import type { Event, EventType } from '$lib/types';
import { goto } from '$app/navigation';
import type { PageData } from '../$types';
import { formatTime, formatDate, isEventInTimeRange } from '$lib/dateHelpers';
import { t } from '$lib/i18n/i18n.js';
import Fuse from 'fuse.js';
@@ -16,7 +15,7 @@
let showFilters = false;
let fuse: Fuse<Event>;
export let data: PageData;
export let data: any;
// Use the server-side data
$: publicEvents = data?.events || [];
@@ -67,8 +66,15 @@
// Sort events by date and time
events = events.sort((a, b) => {
const dateA = new Date(`${a.date}T${a.time}`);
const dateB = new Date(`${b.date}T${b.time}`);
// Parse dates as local timezone to avoid timezone issues
const parseEventDateTime = (event: Event) => {
const [year, month, day] = event.date.split('-').map(Number);
const [hours, minutes, seconds] = event.time.split(':').map(Number);
return new Date(year, month - 1, day, hours, minutes, seconds || 0);
};
const dateA = parseEventDateTime(a);
const dateB = parseEventDateTime(b);
if (selectedSortOrder === 'asc') {
return dateA.getTime() - dateB.getTime();

View File

@@ -86,8 +86,9 @@ export const actions: Actions = {
});
}
// Check if date is in the past (but allow editing past events for corrections)
const eventDate = new Date(date);
// Check if date is in the past using local timezone (but allow editing past events for corrections)
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);