datetime - how do I get the string representation of the users timezone using javascript? - Stack Overflow

I want to output the users system timezone, e.g. "EST". How can I do that, preferably using J

I want to output the users system timezone, e.g. "EST". How can I do that, preferably using Javascript, though I could do it on the PHP side as well, if that's significantly easier.

I want to output the users system timezone, e.g. "EST". How can I do that, preferably using Javascript, though I could do it on the PHP side as well, if that's significantly easier.

Share Improve this question asked Jun 12, 2009 at 16:06 sprugmansprugman 19.8k36 gold badges115 silver badges164 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Javascript has a Date.getTimezoneOffset function which will give you the difference in minutes between the GMT and the local timezone. To get from that to the actual offset, you could say:

var offset = Date.getTimezoneOffset / 60 * -1

The * -1 is needed because the offset will be opposite what you expect (for instance, EST, which is GMT -5, would result in 300, because GMT is 300 minutes ahead).

From there, you will need to convert the offset to the description of the offset. There is no built in way in Javascript to do so. You could consider creating a lookup array, but then there would be the issue that there are multiple names for the same offset (for instance, GMT - 5 could be EST (Eastern Standard Time) or CDT (Central Daylight Time).

See Wikipedia for a list of timezones.

You will have to use Javascript - since you do not know the client timezone on the server side. You could use the code below and extract the relevant timezone string from localtime.

var now = new Date();
localtime = now.toTimeString();

which will return something like "12:21:44 GMT-0400 (EDT)"

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745103232a4611401.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信