Can I get the name of the time zone of the client by using jQuery?
Example: Pacific S.A. Standard Time, Greenwich Standard Time, like this: https://en.wikipedia.org/wiki/List_of_time_zones_by_country
Timezonelist
In javascript , the Date. getTimezoneOffset() method returns the time-zone offset from UTC, in minutes, for the current locale.
Definition and Usage. getTimezoneOffset() returns the difference between UTC time and local time. getTimezoneOffset() returns the difference in minutes. For example, if your time zone is GMT+2, -120 will be returned.
What is a time zone? Earth is divided into 24 segments known as lunes. A time zone is defined as an area where a uniform standard time applies for legal, economic and social purposes. Standard time zones are 15 degrees of longitude wide.
Can I get the name of the time zone of the client by using jQuery?
No. jQuery has only one date/time function, which is $.now()
.
If you meant JavaScript, the answer is still no. You can only obtain a time zone offset from date.getTimezoneOffset()
. You can't get a time zone - at least not in all browsers. Refer to the timezone tag wiki's section titled: "Time Zone != Offset"
You can guess at the time zone, by using the jsTimeZoneDetect library, but it is just a guess. It may or may not be accurate.
You can also now use moment.js with the moment-timezone add on. It now supports time zone guessing with moment.tz.guess()
.
If you can guarantee your users are running in an environment that fully supports the ECMAScript Internationalization API, you can get the user's time zone like this:
Intl.DateTimeFormat().resolvedOptions().timeZone
You can review the compatibility table, under DateTimeFormat
- resolvedOptions().timezone defaults to the host environment
to determine which environments this will work in.
Honestly, the best thing to do is to just give your user a screen somewhere that they can select their timezone. You might use a drop-down list, or you might use a map-based timezone picker - like this one. You can use jsTimeZoneDetect as a default value, but your user should be able to change it.
Also, all of these are going to give you an IANA time zone identifier, such as America/Los_Angeles
. But the examples you gave appear to be Windows time zone ids (for use with TimeZoneInfo
in .net). You should read the timezone tag wiki, and then also this question: How to translate between Windows and IANA time zones?
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