views:

1084

answers:

6

Time zones can be hard to select from dozens of options. I would like to make this process simpler. I saw some forms in the wild that can do the best guess. I Goggled for a solution but with out any luck. Do any one have an idea how to do it?

+6  A: 

Javascript:

var today = new Date();

alert( today.getTimezoneOffset() );

This will give the Offset (GMT-X), but not the actual timezone name. Keep in mind same GMT offset can correspond with multiple timezones and also have to take into account Daylight savings. However, this is probably the easiest place to start, short of giving the users pick their timezone from a dropdown.

Jose Basilio
The only thing I'd like to note is that `getTimezoneOffset()` seems to return the amount of time GMT is the offset from the user's time zone is offset, and not the the other way around.i.e. When in North American Eastern Standard Time,`(new Date()).getTimezoneOffset() / 60` returns `5`, when EST is actually equal to GMT-05:00, not GMT+05:00.
gabriel
+1  A: 

You can use JavaScript to more-or-less reliably determine timezone offset. HTTP, to my knowledge, does not provide any means to determine client timezone on the server.

Anton Gogolev
+1  A: 

Use Javascript:

<input type="hidden" id="visitorTZ" name="visitorTZ" />
<script type="text/javascript">
    d = new Date();
    document.getElementById('visitorTZ').value = d.getTimezoneOffset()/60; 
</script>
vartec
+2  A: 

You could try to detect a users country by matching their IP address to a series of lists. There are different ways to achieve this but the best is by using this API.

An example of usage would be: http://nl.ae/iptocapi.php?type=1&amp;ip=00.00.00.00. This will return the country code of that IP address. Then it is quite easy to match the country code with the timezone.

Once you have found the timezone this way you can use it as the default value in a dropdown box.

Sam152
This isn't going to work. *Many* countries comprise more than one timezone.
Turtle
In retrospect it really wasn't a good idea.
Sam152
A: 

You can get a GeoLocation database from IP2Location where you can check the user’s IP address and you should be able to get the timezone for that IP address.

Kurosaki
+1  A: 

I found a much better and robust solution here: http://www.onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/.

As the author explains, Date.getTimezoneOffset() is inconsistent.

Andy Edinborough