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?
On the
new Date(
) you can get the offset, to get the timezone name you may do:new Date().toString().replace(/(.*\((.*)\).*)/, '$2');
you get the value between
()
in the end of the date, that is the name of the timezone.I realize this answer is a bit off topic but I imagine many of us looking for an answer also wanted to format the time zone for display and perhaps get the zone abbreviation too. So here it goes...
If you want the client timezone nicely formatted you can rely on the JavaScript Date.toString method and do:
This will give you "GMT-0400 (EST)" for example, including the timezone minutes when applicable.
Alternatively, with regex you can extract any desired part:
For "GMT-0400 (EDT)" :
For "GMT-0400" :
For just "EDT" :
For just "-0400":
Date.toString reference: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/toString
you can simply try this. it will return you current machine time
If all you need is the "MST" or the "EST" time zone abbreviation:
Timezone in hours-
If you want to be precise as you mentioned in comment, then you should try like this-
This will do the job.
undefined