How can I gather the visitor's time zone information? I need the Timezone, as well as the GMT offset hours.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
As an alternative to
new Date().getTimezoneOffset()
andmoment().format('zz')
, you can also use momentjs:jstimezone is also quite buggy and unmaintained (https://bitbucket.org/pellepim/jstimezonedetect/issues?status=new&status=open)
Using an offset to calculate Timezone is a wrong approach, and you will always encounter problems. Time zones and daylight saving rules may change on several occasions during a year, and It's difficult to keep up with changes.
To get a correct timezone in JavaScript, you should use
Unfortunately, by the time of writing this, it's not yet widely supported.
I know it's working correctly at least in Chrome.
ecma-402/1.0 says that
timeZone
may be undefined if not provided to constructor. However, future draft (3.0) fixed that issue by changing to system default timezone.in ecma-402/3.0 which is still in a draft it changed to
So before using it check current support across browsers for example at:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/resolvedOptions
try
getTimezoneOffset()
of theDate
object:This method returns time zone offset in minutes which is the difference between GMT and local time in minutes.
JavaScript:
Use this to convert OffSet to postive: