Theo's Typesafe CultTTC
Theo's Typesafe Cult16mo ago
1 reply
George

Handling Dates with client and server and database

Hello, so i am trying to create an appointment scheduling website and I am having trouble using dates between the client and the server.

When I first implemented my endpoints, I made sure to convert any date I pass from the client to the server using a zod schema that converts said date to UTC for me.

Then I deal with my app logic (using date-fns to handle dates) and save the date or make any db queries using this UTC date. This was working completely fine when working locally.

But once I deployed the app, the dates for available appointment slots were completely different. I assumed it was because the timezone my deployed server is a different timezone to the client.

Now I think that the date modifications I am making within my endpoint logic is invalid since I am using date-fns and it is calculating the modifications based on the server's timezone.

So I am wondering what the best practise is to handling dates between client and server.
Should i just not use date-fns and handle dates on my own? (Could not find an easy way to use date-fns for a specific timezone)
Should I accept a date as it is and only convert to UTC once I need to query or enter data into the database?

Thanks for the help.
Was this page helpful?