How do you retrieve the client user's time zon

2019-02-22 20:44发布

The question is as it sounds - how to retrieve the client user's time zone based on their browser input? If it matters, server side, J2EE is used - but I don't think that really matters in this case. Right now, HTTP is the protocol, it may use HTTPS when it goes live.

5条回答
姐就是有狂的资本
2楼-- · 2019-02-22 21:34

As stated by Pierre-Luc Simard, which is a very good idea, you can use the IP to deduce that information. Credit goes to him but I'll add another link to a free alternative which is Geolite from Maxmind. There's also a sample code in PHP for the timezones and I'm pretty sure you'll find the equivalent in their Java API.

查看更多
何必那么认真
3楼-- · 2019-02-22 21:35

Seems somebody hacked some javascript to deduce this but yes, the best way is to let the user decide. But you can use the above link to choose the default for the user.

查看更多
爷的心禁止访问
4楼-- · 2019-02-22 21:35

I'm sure this is a duplicate.

The best way is to ask the user. Time Zone is not sent in the HTML headers, by default. Also consider that a user from one time zone may be visiting another. Which time zone should you use?

查看更多
戒情不戒烟
5楼-- · 2019-02-22 21:41

You can also use IP geolocation services like ip2location. They sell a database subscription that will return the timezone in which the IP address is located. Of course, this has a price. A Javascript solution would work but does require that the client supports it.

The geolocation will support any client but can provide the wrong result depending on how the IP block was attributed or if the client is using a proxy server located in a different timezone (most likely an anomysing proxy service). The Javascript solution will support a large variety of client but will return the timezone base on what the computer is set to and not the timezone base on the actual location (traveling user don't always change the time on their computer). So there is no single solution that covers them all.

Using one of these two methods to make a guess and then allowing the user to correct it would be the best approach I would think. If you want to go overkill use both and let the user decide which makes most sense.

查看更多
登录 后发表回答