19

I am using the Intl API. In Chrome:

 Intl.DateTimeFormat().resolved.timeZone

Returns "Europe/London"

In Firefox this return undefined, even though Firefox supports Intl.

How can I get a timezone with Firefox?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
mikemaccana
  • 110,530
  • 99
  • 389
  • 494

1 Answers1

34

Technically, the more correct incantation is:

Intl.DateTimeFormat().resolvedOptions().timeZone

Unfortunately, not all implementations currently support the time zone features of the Intl API. So while this will return a valid IANA time zone in Chrome, it won't yet do that in FireFox and several other browsers.

The kangax Intl compatibility table shows this feature as a sub-item under the DateTimeFormat object labeled resolvedOptions().timeZone defaults to the host environment.

See the following FireFox work items:

  • Bug 1158733 - Detect system timezone from Javascript Internationalization API
  • Bug 895737 - DateTimeFormat doesn't track current time zone
  • Bug 837961 - Add support for IANA time zone names to internationalization API

In the meantime, there are two viable alternatives:

  • jsTimeZoneDetect - which is focused soley on the task of time zone guessing.
  • moment-timezone - which is an add-on to the popular moment.js library, and contains an API moment.tz.guess() to try to guess the user's time zone.

Note that both of these will internally try to use the Intl API if it is available and functioning, before trying additional guessing algorithms.

Also note that the best practice is to never rely solely on time zone detection / guessing, but rather to use it to select a sensible default value from a time zone picker control. Always give the user some way to choose their desired time zone. This is important, as the time zone they presently have their computer set to may or may not be the time zone they'd like to use within your application. For example, it's common for web-based calendaring software (such as Google Calendar, or Outlook.com) to allow the user to set a time zone in their user profile.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • When I try to get on Firefox it doesn't show the IANA code, but "Etc/+9GMT". Is possible to get the IANA instead? – Igor O Oct 24 '17 at 07:01
  • Are you sure that's exactly what's returned? If you were in a UTC-9 time zone without DST, a response of `Etc/GMT+9` would be a valid IANA time zone. But what you wrote has characters out of sequence. – Matt Johnson-Pint Oct 24 '17 at 15:31
  • Sorry, my mistake. It always returns `Etc/GMT-9`, not the IANA code `Asia/Seoul` as expected. In Chrome 64bits Mac 61.0.3163.100 and Safari work perfectly. Only Firefox it returns like this. (using moment.tz.guess() or jstz.determine().. both return like that) – Igor O Oct 25 '17 at 02:10
  • I think the problem was solved a few days ago. The problem was that they updated the main version but not the /dist versions. Solved. – Igor O Oct 26 '17 at 04:40
  • Good to know they improved it. Though do note the other response was still a valid IANA time zone identifier. It just doesn't have the history of South Korea before 1989. (The inverted sign is normal). – Matt Johnson-Pint Oct 26 '17 at 05:36
  • 1
    At this day, is completely supported by Chrome, Firefox and major browsers. – Alessandro_Russo Jun 20 '23 at 15:11