简体   繁体   English

如何通过java脚本查找系统区域时区名称

[英]how to find the the system regional time zone name by java script

Hi i have a typical requirement in my web application, i have to fill a dropdown box with all system timezones and set the selected time zone where from the client is browsing. 您好我的网络应用程序中有一个典型的要求,我必须填写一个包含所有系统时区的下拉框,并设置从客户端浏览的所选时区。

Only timezoneoffset is not enough to handle this suituation, since there are two reasons 1.the latest OS having more than one time zone with the same offset 2.If Day light save is enabled or disable(can be detected from Javascript) the offset will vary. 只有timezoneoffset不足以处理这种情况,因为有两个原因1.最新的操作系统有多个具有相同偏移量的时区2.如果启用或禁用日光保存(可以从Javascript检测到)偏移将变化。

is ther any ways of handling this in Java script? 有没有办法在Java脚本中处理这个?

Short answer : Use this open source JavaScript project: jsTimezoneDetect . 简短回答 :使用这个开源JavaScript项目: jsTimezoneDetect It is based on the code that MK linked here, which is great, but not enough since different regions within the same timezone start their daylight savings at different times. 它基于MK链接的代码,这很好,但还不够,因为同一时区内的不同区域在不同时间开始夏令时。 Hemisphere also plays a factor. 半球也是一个因素。

Long answer : One example is the -07:00 UTC timezone which basically has three sub timezones. 答案 :一个例子是-07:00 UTC时区,基本上有三个子时区。

  • US Mountain Standard Time (no daylight saving... this is Arizona) 美国山地标准时间(没有夏令时......这是亚利桑那州)
  • US Mountain Time (with daylight savings) 美国山区时间(夏令时)
  • Mexico Mountain Time (with daylight savings) 墨西哥山区时间(夏令时)

Assuming that you know your user's UTC offset you will now have to check whether your timezone uses daylight savings. 假设您知道用户的UTC偏移量,您现在必须检查您的时区是否使用夏令时。 Create one January and one June date and check these two dates. 创建一个1月和6月的日期并检查这两个日期。 If their UTC offsets differ, then you are in a timezone that uses daylight savings. 如果他们的UTC偏移量不同,那么您处于使用夏令时的时区。

Say that you have determined that your user is -07:00 with daylight savings, that means either US Mountain Time (America/Denver) or Mexico Mountain Time (America/Chihuahua). 假设您已确定您的用户是夏令时-07:00,这意味着美国山地时间(美国/丹佛)或墨西哥山区时间(美国/吉娃娃)。 How to tell them apart? 如何区分他们? Well, the date they start their daylight savings differ. 那么,他们开始夏令时的日期各不相同。

US daylight savings starts in 2011 starts 13 March, and in Mexico it 4th April. 美国夏令时从2011年3月13日开始,墨西哥4月4日开始。 So start with Denver and check if 13 march is summer time, no? 那么从丹佛开始,检查13月3日是夏令时,不是吗? Ok, check if 4th April is summer time. 好的,检查一下4月4日是夏天的时间。 Yes? 是? Bingo. 答对了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM