diff --git a/components/CreateEventDialog.vue b/components/CreateEventDialog.vue index 2cf5a74..6fdf273 100644 --- a/components/CreateEventDialog.vue +++ b/components/CreateEventDialog.vue @@ -489,19 +489,15 @@ watch(startDate, (newStartDate) => { } }); -// Watch for separate date and time changes to combine them +// Watch for separate date and time changes to combine them using robust parsing watch([startDate, startTime], ([newDate, newTime]) => { if (newDate && newTime) { console.log('[CreateEventDialog] Combining start date/time:', { date: newDate, time: newTime }); - // Ensure time is in 24-hour format (HH:MM) - const normalizedTime = normalizeTimeFormat(newTime); - console.log('[CreateEventDialog] Normalized start time:', normalizedTime); + const combinedDateTime = createDateTimeFromParts(newDate, newTime); - const combinedDateTime = new Date(`${newDate}T${normalizedTime}`); - - if (isNaN(combinedDateTime.getTime())) { - console.error('[CreateEventDialog] Invalid start datetime created:', { date: newDate, time: newTime, normalized: normalizedTime }); + if (!combinedDateTime) { + console.error('[CreateEventDialog] Failed to create start datetime from parts:', { date: newDate, time: newTime }); return; } @@ -514,14 +510,10 @@ watch([endDate, endTime], ([newDate, newTime]) => { if (newDate && newTime) { console.log('[CreateEventDialog] Combining end date/time:', { date: newDate, time: newTime }); - // Ensure time is in 24-hour format (HH:MM) - const normalizedTime = normalizeTimeFormat(newTime); - console.log('[CreateEventDialog] Normalized end time:', normalizedTime); + const combinedDateTime = createDateTimeFromParts(newDate, newTime); - const combinedDateTime = new Date(`${newDate}T${normalizedTime}`); - - if (isNaN(combinedDateTime.getTime())) { - console.error('[CreateEventDialog] Invalid end datetime created:', { date: newDate, time: newTime, normalized: normalizedTime }); + if (!combinedDateTime) { + console.error('[CreateEventDialog] Failed to create end datetime from parts:', { date: newDate, time: newTime }); return; } @@ -578,14 +570,76 @@ const normalizeTimeFormat = (timeString: string): string => { // Ensure time is in HH:MM 24-hour format if (!timeString) return ''; - // If already in HH:MM format, return as is - if (/^\d{2}:\d{2}$/.test(timeString)) { - return timeString; + // Remove any whitespace + const cleaned = timeString.trim(); + + // If already in HH:MM format, validate and return + if (/^\d{2}:\d{2}$/.test(cleaned)) { + const [hours, minutes] = cleaned.split(':').map(Number); + if (hours >= 0 && hours <= 23 && minutes >= 0 && minutes <= 59) { + return cleaned; + } } - // Handle other potential formats (this is basic, can be expanded) - // For now, assume HTML time input gives us the correct format - return timeString; + // If in H:MM format, pad with zero + if (/^\d{1}:\d{2}$/.test(cleaned)) { + const [hours, minutes] = cleaned.split(':'); + const paddedHours = hours.padStart(2, '0'); + const numHours = Number(paddedHours); + const numMinutes = Number(minutes); + if (numHours >= 0 && numHours <= 23 && numMinutes >= 0 && numMinutes <= 59) { + return `${paddedHours}:${minutes}`; + } + } + + // Handle seconds format HH:MM:SS by truncating + if (/^\d{2}:\d{2}:\d{2}$/.test(cleaned)) { + const timePart = cleaned.substring(0, 5); + return normalizeTimeFormat(timePart); + } + + console.error('[CreateEventDialog] Invalid time format:', timeString); + return ''; +}; + +const createDateTimeFromParts = (dateString: string, timeString: string): Date | null => { + if (!dateString || !timeString) { + return null; + } + + try { + // Normalize the time format first + const normalizedTime = normalizeTimeFormat(timeString); + if (!normalizedTime) { + console.error('[CreateEventDialog] Failed to normalize time:', timeString); + return null; + } + + // Split time into parts + const [hours, minutes] = normalizedTime.split(':').map(Number); + + // Create date object from date string (YYYY-MM-DD format) + const dateObj = new Date(dateString + 'T00:00:00'); + + if (isNaN(dateObj.getTime())) { + console.error('[CreateEventDialog] Invalid date string:', dateString); + return null; + } + + // Set the time components + dateObj.setHours(hours, minutes, 0, 0); + + // Validate the final result + if (isNaN(dateObj.getTime())) { + console.error('[CreateEventDialog] Invalid combined datetime:', { dateString, timeString, normalizedTime, hours, minutes }); + return null; + } + + return dateObj; + } catch (error) { + console.error('[CreateEventDialog] Error creating datetime:', error, { dateString, timeString }); + return null; + } }; // Validation functions @@ -603,22 +657,13 @@ const validateEndTime = () => { // Only validate if start and end are on the same date if (startDate.value === endDate.value) { - // Normalize time formats before creating Date objects - const normalizedStartTime = normalizeTimeFormat(startTime.value); - const normalizedEndTime = normalizeTimeFormat(endTime.value); - - // Convert times to comparable format by creating full datetime objects - const startDateTime = new Date(`${startDate.value}T${normalizedStartTime}`); - const endDateTime = new Date(`${endDate.value}T${normalizedEndTime}`); + // Use robust parsing to create datetime objects + const startDateTime = createDateTimeFromParts(startDate.value, startTime.value); + const endDateTime = createDateTimeFromParts(endDate.value, endTime.value); // Check if dates are valid - if (isNaN(startDateTime.getTime()) || isNaN(endDateTime.getTime())) { - console.error('[CreateEventDialog] Invalid datetime created in validation:', { - startDateTime: startDateTime.toString(), - endDateTime: endDateTime.toString(), - normalizedStartTime, - normalizedEndTime - }); + if (!startDateTime || !endDateTime) { + console.error('[CreateEventDialog] Failed to create datetime objects in validation'); return true; // Return error if dates are invalid } @@ -712,30 +757,30 @@ const handleSubmit = async () => { loading.value = true; try { - // Combine date and time properly - const startDateTime = new Date(`${startDate.value}T${startTime.value}`); - const endDateTime = new Date(`${endDate.value}T${endTime.value}`); + // Combine date and time properly using robust parsing + const startDateTime = createDateTimeFromParts(startDate.value, startTime.value); + const endDateTime = createDateTimeFromParts(endDate.value, endTime.value); console.log('[CreateEventDialog] DateTime validation:', { startDate: startDate.value, startTime: startTime.value, endDate: endDate.value, endTime: endTime.value, - startDateTime: startDateTime.toISOString(), - endDateTime: endDateTime.toISOString(), - startTimestamp: startDateTime.getTime(), - endTimestamp: endDateTime.getTime() + startDateTime: startDateTime?.toISOString(), + endDateTime: endDateTime?.toISOString(), + startTimestamp: startDateTime?.getTime(), + endTimestamp: endDateTime?.getTime() }); // Validate dates are valid - if (isNaN(startDateTime.getTime())) { - errorMessage.value = 'Invalid start date or time'; + if (!startDateTime) { + errorMessage.value = 'Invalid start date or time format'; loading.value = false; return; } - if (isNaN(endDateTime.getTime())) { - errorMessage.value = 'Invalid end date or time'; + if (!endDateTime) { + errorMessage.value = 'Invalid end date or time format'; loading.value = false; return; }