DEV Community

Cover image for 3 simple rules for effectively handling dates and timezones

3 simple rules for effectively handling dates and timezones

Cory Keane on September 09, 2019

When asked if they struggle with handling dates and timezones, more than 50% of developers responded "YES". My guess is that the people who said "N...
Collapse
 
thedfresh profile image
thedfresh

Storing datetimes in UTC is good practice, unless you have to store future datetimes, in which case you need to save the "clock time" and the time zone, in case that time zone changes DST in the future. Kind of edge-casey but countries change their DST dates all the time, and sometimes drop in/out completely.

Collapse
 
garzo profile image
Dave MG

Asking as an Ops engineer, this sounds relevant and something to have in the "nice to know" mental-stack-but what would be an example edge-case you speak of here?

Collapse
 
thedfresh profile image
thedfresh • Edited

Man, I wrote some massive emails trying to work through this once upon a time, but here's a pretty simple way to think about it.

In 2005 the US moved the end of DST from the last Sunday in October to the first Sunday in November starting in 2007. So the UTC datetime for 8am November 1 in Boston in 2006 was 2006-11-01T13:00:00+00:00, but 2007-11-01T12:00:00+00:00 the following year. This is never a problem for historical dates because your TZ library knows what the UTC/DST offset was for any time zone on any date. What it does not know is what the UTC/DST offset will definitely be on future dates.

Collapse
 
ssimontis profile image
Scott Simontis

I also set all my server clocks to UTC. Especially with cloud applications, it is extremely valuable. If you happen to dual-boot Windows and Linux, Windows isn't going to like it and you have to tweak a registry setting. I have forgotten that arcane registry magic but if anyone needs it I will look it up.

Collapse
 
nyanchev profile image
Nikola Yanchev

Yes please :), not long ago i started dual booting and its kind of anoying having windows clock always wrong

Collapse
 
tonygraycanada profile image
Tony Gray

Good rules generally, but don't forget that if you have to do time aggregation to longer time periods in your business logic, those have to be in local time. Alas, we can't keep everything on the server TZ agnostic.

Collapse
 
atdotde profile image
Robert C. Helling

I think you should have mentioned rule #0: Don't try to hack it yourself, use a library. There are far too many traps to fall into, like "in two days" is the same as "in 48 hours" but could as well be 47 or 49 hours.

And to complicate things there is also what I would call "subjective time" as I had to learn when contributing to Subsurface, a scuba diving log: Divers often do their dives at holiday destinations with time zones different from their home. They want to log things like "I did a dive at 2 p.m.". That is obviously in the time zone of the dive site. But it should still be "2 p.m." when they look at their log at home even when this is in a different time zone. This sounds like one could ignore time zones all together but unfortunately there are other relevant times that should be time zone aware like the above "let's check again for updates in two days" or when interacting with other devices like dive computers or cameras to match pictures takes to the dives in the log. In short: it's a pain!

Collapse
 
gadrawingz profile image
Gad Iradufasha • Edited

Thanks for Recommendation, I been struggling with date and time (with timezone)
[in my Q&A site Donnekt]
I tried to make system to help me with it, But the task was tiresome!
I think using (Moment.js) will be better.

Collapse
 
vianeltxt profile image
vianel

nice post! I guess I would add only one more thing is always use long format for dates

Collapse
 
brendalimon profile image
Brenda Limón

Me too :D

Collapse
 
motss profile image
Rong Sen Ng

Use Intl.

Collapse
 
chrisachard profile image
Chris Achard

AH! timezones are my developer nemesis 🤣 So many little, difficult-to-track-down bugs can be caused by timezones and daylight savings time... UGH!

Good points in your post :)

Collapse
 
hanzlaharoon profile image
Hanzla Haroon

Don't use moment.js, it's outdated and no longer maintained. Instead, use day.js, a fast and modern alternative.