how to handle DST and/or timezones on javascript?

Im building an application where users can schedule and reserve parkings for their cars. I currently manage correctly (i guess) the timezone changes, i.e the schedules are saved on UTC-0 in the database and its shown on the local timezone for the user on clientside. The issue here is daylight saving times. On 05/04/2025, in Chile the hour would set us at UTC-4 instead of UTC-3, so every schedule that was previously saved for, i.e 15:00 on saturday, saved on its UTC-0 representation at the database, would (i think) look like 14:00, and that's not how it should be. Also, there is a quite weird bug (that im currently fixing) where if i save schedules from now 04/04/2025 till 31/04/2025, the day 05/04/2025 (when the DST changes) is repeated and the last day isnt 31 but 30. But that is from another issue so it doesnt really matter, just saying hehe
2 Replies
yaroslav.korotaev
Probably there is no single anwser to that. But here some thoughts on it, hope these could be useful. 1. Save both a UTC timestamp + desired timezone. Not UTC+N, but something like EEST/CST, which is related to location and it's daylight saving policies. So you can infer specific time shift at specific date when needed by looking up timezone database; 2. Store only schedule parameters, not pregenerate events upfront. Starting point + timezone + repeat pattern + (maybe) amendments. Generate series of events only on frontend just for viewing. Save only past/commited events in database for reporting, linked back to original schedule; 3. For simple repeated daily intervals it may be useful to store date and time separately. Like date = "2025-04-05", start = 1400 (14:00), duration = 100 (1h). So you can calculate final timestamps by advancing date to +N days and building timestamp for specified date, time and timezone. Tinkering with date-fns could also be helpful – https://date-fns.org/v4.1.0/docs/Time-Zones
rNdom
rNdomOP5d ago
thank you very much! i ended up looking at how the schedules and dates were behaving and it turns out that the native js functions to create and get dates already had dst implemented. For example if i created a series of schedules from 04 to 07, it would look like that 04-04-2025T00:00:00:000Z but from 5 and above it looks like this 04-04-2025T01:00:00:000Z. So when user creates future schedules they already hace dst considered

Did you find this page helpful?