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?
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:
In the meantime, there are two viable alternatives:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With