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?
It's already been answered how to get offset in minutes as an integer, but in case anyone wants the local GMT offset as a string e.g.
"+1130"
:I was looking for just the 3 letter string (like "PDT") and tried Marquez's answer but had to tweak it a bit for cross browser support. Luckily, the string is the last potential match :)
Here's what I did, in CoffeeScript:
Works in IE8, IE9, Safari 9, Firefox 44, and Chrome 48
This value is from user's machine and it can be changed anytime so I think it doesn't matter, I just want to get an approximate value and then convert it to GMT in my server.
For example, I am from Taiwan and it returns "+8" for me.
Working example
JS
HTML
Result
You just to to include moment.js and jstz.js
and after that
I wrote a function in my project, which returns the timezone in
hh:mm
format. I hope this may help someone:Working Fiddle
See this resultant operator was opposite to the Timezone .So apply some math function then validate the num less or more.
See the MDN document