Search code examples
javascriptgeolocationtimezone

JavaScript's time zones as a way to locate a user's country


I need to determine a user's country, and it doesn't need to be super accurate (getting it right 90% of the time is probably enough). I realized the time zone returned by JavaScript could work.

Example:

console.log(Intl.DateTimeFormat().resolvedOptions().timeZone);

Will return America/Toronto or America/New_York, which can help me differentiate a Canadian from an American.

Sure I understand this is based on the user’s preferences, not its real location. But is there a huge pitfall I'm not seeing right now using this technic?


Solution

  • After some research, turns out it depends on the scenario (was predictable). For my scenarios, American get the right Timezone 95%+ of the time, while Canadian get it 90%+.

    The way to test this out was to save the user's JS timezone in our Google Analytics, and then compare it to their location.

    So your mileage may vary, but for my use case, this was more than enough.