Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Timezone using Intl API doesn't work in Firefox:

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?

like image 398
mikemaccana Avatar asked Jun 13 '16 20:06

mikemaccana


1 Answers

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.

like image 103
Matt Johnson-Pint Avatar answered Oct 13 '22 01:10

Matt Johnson-Pint