All web developers at some point will run into time zones and dealing with time zones can be tricky. Here is some foundation that might help:
Most applications have a sensible default of working with time in UTC Coordinated Universal Time. There is generally system time, application time, and database time. UTC is sometimes used interchangeably with GMT. UTC is a time standard, GMT is a time zone.
But there are good reasons for web applications to use the client's time zone. Here are some applications where using the user's time zone is critical to the functionality: calendar and appointment scheduling, trading platform, daily habit tracking app.
In order to use the correct time zone for the user, we have two options - ask the user to select a timezone (from a dropdown form) and store it in DB or auto detect the user's timezone based on their browser and operating system.
The later way is an approximation and cannot be 100% reliable because it's possible that the user has not configured the correct time on their machine or the user is behind a proxy or VPN and their real location is hidden.
Here is a how you can select time zone using modern ECMAScript:
console.log(Intl.DateTimeFormat().resolvedOptions().timeZone);
America/Chicago
You could store this in a cookie
and pass it along to your backend where it can be used during requests.
That leaves asking the user as the most reliable way. How can you effectively ask users for their timezone. There is an offician list of strings IANA Time Zone Database. This can be stored in the user profile and the user can have control over changing their time zone if they are traveling.
Top comments (0)