Hey everyone! I just deployed my Java Spring Boot backend on Render.com, and I've noticed that events on my calendar page (the frontend is built with Next.js) are sometimes showing up hours late or even on the wrong day. For example, an event that should be at 9:00 PM on the 18th is now displaying as 1:00 AM on the 19th.
It looks like my MongoDB data is stored in UTC, and when I was running everything locally, everything worked perfectly fine using LocalDateTime, which matched my system's local time. Now that the server is on UTC, it seems like LocalDateTime is causing issues because it doesn't reflect my actual timezone anymore.
I read some articles suggesting that I switch to OffsetDateTime on the backend and then handle date formatting on the frontend using JavaScript's Date object. Is this the right way to go, or are there better options? I don't have much experience with this, so any advice would be greatly appreciated! Thanks!
4 Answers
Another approach is to just convert the UTC date from your backend to the local date on the frontend using JavaScript. The built-in Date object can handle that for you with something like new Date(). Usually, backends store the time in UTC and the frontend just adapts it to the user's timezone.
You might want to consider storing times in epoch format. That way, you can handle the display on the client side. It could make things a bit simpler!
Always make sure to use a timezone-aware date format! If you're in Java, go with OffsetDateTime. In JavaScript, check out Luxon—it's a powerful tool for managing dates compared to the standard Date object. Also, if you serialize dates, use ISO 8601 strings. This prevents many common errors related to timezones, which can save you from a lot of future headaches!
Using OffsetDateTime is definitely a solid choice! If you want to keep it simple, you could also configure the timezone manually in your JVM to match your local timezone. Check out the Java docs for how to set that up, it might save you some hassle!

Absolutely! The only exception might be things like birthdays that aren't influenced by timezones. We often refer to these as 'naive' dates versus 'aware' dates.