1

enter image description hereI have some timezone saved in my profile as "America/New York". When my device moves to "Dubai" then device timezone is changed while profile timezone is still "America/New York".

(1) How to autodetect and prompt user that his device timezone has changed and is different from profile/saved timezone? The user is accessing app through channels like web, android and ios device as well.

(2) How to get list of available timezones so that when prompted for timezone change, he can manually update profile timezone same as device timezone. This timezone list should be same for all devices like web, android and ios device.

The issue here, is some timezone returns "Asia/Kolkata" and "Asia/Calcutta". Since to synchronize all devices regarding timezone. How to get list of available timezones same for all devices.

Mahi
  • 3,748
  • 4
  • 35
  • 70

2 Answers2

2

How to autodetect and prompt user that his device timezone has changed and is different from profile/saved timezone?

Time zone detection is covered in this answer. There are multiple approaches, but ultimately you want to get the IANA time zone identifier (such as America/New_York) to compare against your saved value in the user's profile.

How to get list of available timezones...

JavaScript doesn't have a built-in method for this, but you can use a library. For example, Moment-timezone provides the moment.tz.names() function, and Date-fns-timezone provides the listTimeZones() function.

... This timezone list should be same for all devices like web, android and ios device.

While most environments use IANA time zone identifiers, there is no guarantee that all devices will have fully updated data. Say a new time zone is introduced and your devices detect it - if your server-side platform doesn't have the latest time zone data, then you might encounter an error. The best thing you can do here is to make sure you regularly check for updates, which varies depending on platform.

... some timezone returns "Asia/Kolkata" and "Asia/Calcutta"

That is fine. Asia/Kolkata is the prefered canonical zone, and Asia/Calcutta is a link (or alias) of that zone. All modern platforms should be able to interpret either. If you're trying to do this yourself, you'll need to be sure to resolve links to their canonical zones before comparing. There are libraries that can do this for you.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Thanks Matt, I am using jstz.min.js for timeZone detection and jstz.determine().name() returns "Asia/Kolkata" while the server returns profile timezone as "Asia/Calcutta" . So when I am matching the two strings, getting timezone change prompt everytime. – Mahi Mar 02 '19 at 18:42
  • Is string matching is a good idea or can you suggest and guide me some best solution to this problem. – Mahi Mar 11 '19 at 14:48
  • I wrote a question and answer for that [here](https://stackoverflow.com/questions/55110347/how-to-determine-if-two-iana-time-zones-are-links-in-javascript). – Matt Johnson-Pint Mar 11 '19 at 21:13
0

The frequency of the check should be based on your own analysis: if the timezone should be automatically updated (or the user should be prompted almost immediately to update it's own timezone) a setInterval that performs a check each minute should be the best option.

I think that the best option is to use a time parameter instead of the matching string, like the offset expressed in minutes if compared to the UTC. Actually, JavaScript comes in handy with a quite useful method placed into the DateTime object:

setInterval(() => {
    let dt = new Date();

    if(dt.getTimezoneOffset() !== localStorage.get('tzOffset') {
        // redirect to a proper component or automatically update the tz
    }
}, 60000);

I am assuming that you are storing the current timezone in the local storage at the key 'tzOffset'. getTimezoneOffset returns the difference in minutes between UTC time and local time, accprdingly to the MDN

For what regards the other question, using moment timezones could help a lot to obtain a properly formatted select on different devices:

for(let tz of moment.tz.names()) {
    // do your stuff wuth the timezone name
}
Andrea Alhena
  • 986
  • 6
  • 9
  • 1
    The problem with this approach is that the user will get prompted simply when the offset changes within their time zone, such as happens twice annually in time zones that have daylight saving time. See "Time Zone != Offset" in [the timezone tag wiki](https://stackoverflow.com/tags/timezone/info). – Matt Johnson-Pint Feb 25 '19 at 18:19