3 ms·
It's pretty simple once you know the rules: - Always store times and calculate differences/offsets in UTC. - Never attempt to convert a local time to UTC (it
by tadfisher 14y ago
It's pretty simple once you know the rules:
- Always store times and calculate differences/offsets in UTC.
- Never attempt to convert a local time to UTC (it is not a one-to-one mapping!).
- Depending on your application, convert any dates to a given timezone at the moment they are displayed.
Now Javascript throws a monkey-wrench into the whole affair, because it assumes Dates should be handled in the user's local timezone, which, if you're dealing with timezone issues, is certainly not what you want. So attempting to convert a Date object from one timezone to the next is surely impossible to do without introducing bugs unless you understand the rules for every timezone you're dealing with.
Luckily, a guy named Olson created a database of timezone rules for us mortals to use in our timezone-aware applications. In another stroke of good fortune, some smart people wrote a Javascript library [1] that parses this database for you and can convert your Javascript Date object between any timezone on the planet. So you do all your calculations using this Date object in UTC mode, then convert to the desired timezone at the time they are displayed.
[1] https://github.com/mde/timezone-js https://github.com/mde/timezone-js