Users were reporting that times/schedules were not being saved and/or displayed in the correct timezone. Sessions created for specific times (e.g., 2:30 PM) were showing up at completely different times (e.g., 6:30 AM or 10:30 PM).
The issue was in src/lib/care-schedules.ts - specifically in two functions:
createCareSchedule()- When creating one-time sessionsgenerateSessionsFromSchedule()- When generating sessions from recurring schedules
The code was using JavaScript's setHours() method on the server side:
// ❌ INCORRECT CODE (before fix)
const sessionStart = new Date(startDate);
sessionStart.setHours(startHour, startMinute, 0, 0); // Uses SERVER's timezone!When code runs on the server (marked with "use server"), setHours() interprets the time in the server's timezone, not the user's timezone.
Example of the problem:
- User in PST (UTC-8) creates a session for 2:30 PM
- Server is running in UTC timezone
- Server creates date as 2:30 PM UTC
- User sees 6:30 AM PST (8 hours off!)
Or if server is in a different timezone:
- User in EST creates session for 2:30 PM
- Server in PST interprets as 2:30 PM PST
- User sees 5:30 PM EST (3 hours off!)
Changed the code to use the existing datetimeLocalToUTC() function which properly handles timezone conversion:
// ✅ CORRECT CODE (after fix)
const sessionStart = datetimeLocalToUTC(`${startDate}T${startTime}`);
const sessionEnd = datetimeLocalToUTC(`${startDate}T${endTime}`);This function:
- Takes a datetime-local string (e.g., "2024-01-15T14:30")
- Interprets it as being in the user's local timezone
- Converts it to UTC for storage in the database
- PostgreSQL stores it as TIMESTAMPTZ (with timezone info)
- Line ~172-175: Fixed
createCareSchedule()for one-time sessions - Line ~358-369: Fixed
generateSessionsFromSchedule()for recurring sessions
- Line ~2: Added import for
startOfDayUTCandendOfDayUTC - Line ~46-47: Updated
getSessionsForDay()to use timezone-aware helpers
- Updated documentation to explain the actual bug and fix
- User enters: "January 15, 2024" and "2:30 PM" in their browser (PST)
- Form sends:
startDate = "2024-01-15"andstartTime = "14:30" - Server combines:
"2024-01-15T14:30"(datetime-local format) datetimeLocalToUTC()converts: Interprets as 2:30 PM PST → UTC (10:30 PM UTC)- Database stores:
2024-01-15 22:30:00+00(UTC with timezone) - User sees: 2:30 PM PST ✅ Correct!
- User in different timezone sees: Correct time for their timezone ✅
- Database returns:
2024-01-15 22:30:00+00(UTC) - JavaScript Date: Receives as Date object (internally UTC)
- Browser displays: Automatically converts to user's local timezone
- User sees: 2:30 PM PST (if they're in PST) ✅
Please verify the following scenarios:
- Create a one-time session with specific start/end times
- Verify the times display correctly in the session list
- Verify the times are correct when editing the session
- Create a recurring schedule (e.g., every Monday, 9:00 AM - 5:00 PM)
- Generate sessions for a date range
- Verify all generated sessions show correct times
- Check that sessions appear at the correct times in month view
- Check that sessions appear at the correct times in week view
- Check that sessions appear at the correct times in day view
- Record a drop-off time
- Record a pick-up time
- Verify these times display correctly in session details
- If you have access to devices in different timezones, verify times display correctly relative to each timezone
- Sessions created in one timezone should show the correct equivalent time in another timezone
The previous fix in January 2025 updated the database schema to use TIMESTAMPTZ instead of TIMESTAMP. This was necessary but not sufficient to fix the problem.
The database fix ensured timezone data was stored properly, but the application code was still creating dates incorrectly by using setHours() on the server side.
Both fixes were needed:
- ✅ Database schema:
TIMESTAMPTZ(3)columns (already applied) - ✅ Application code: Use
datetimeLocalToUTC()for all server-side date creation (NOW FIXED)
-
Server vs Client Code: Always use
datetimeLocalToUTC()when creating dates/times in server-side code (functions marked with"use server") -
Existing Data: Sessions created before this fix may have incorrect times. You may need to:
- Manually review and correct any upcoming sessions
- Or regenerate sessions from schedules
-
Form Inputs: HTML datetime-local inputs work correctly because they send strings like "2024-01-15T14:30" which are then properly converted by
datetimeLocalToUTC() -
Database Queries: Date range queries now use
startOfDayUTC()andendOfDayUTC()helpers for consistency
- See
TIMEZONE_FIX.mdfor detailed technical explanation - See
src/lib/datetime.tsfor utility functions and their documentation - See
DATABASE_SCHEMA.mdfor schema documentation
All timezone issues related to session creation and display have been resolved. Sessions are now correctly saved and displayed in the user's local timezone, regardless of where the server is running.