Detecting a users Country with Timezone

I recently stumbled across a stack overflow comment when doing some geolocation research that lead me to this bit of JavaScript.


It seems like a low stakes (and probably low reliability) way to detect a users country without any 3rd party, api, or permission request. Browser support is pretty good too. The stack overflow link above contains a really detailed codepen to detect many countries, but let’s keep it simple and just try to detect one.

Let’s use my country of Canada as an example. Create an array of timezones from the tz database and check the users timezone to see if it’s contained in the array.

Here’s a really quick and dirty test:

<p>Are you in Canada?: <span id="country">Unknown</span></p>

    let tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
    let canadianTzs = [
    if (tz) {
      let tzspan = document.querySelector("#country");
      if (tzspan) {
        let answer = "Unknown";
        if (canadianTzs.includes(tz)) {
          answer = "Yes";
        } else {
          answer = "No";
        tzspan.textContent = answer;

The result:

Are you in Canada?: Unknown

Here’s the codepen from that inspiration stack overflow post that has a much more comprehensive country detector: