Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect user time-zone in sign up forms?

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?

like image 822
Tom S. Avatar asked Apr 20 '09 12:04

Tom S.


People also ask

How do I find user timezone?

The client's timezone offset could be detected by using the Date object's getTimezoneOffset() method. The getTimezoneOffset() method returns the time difference between UTC time and local time, that is the time offset, in minutes.

How do I get local timezone offset?

The JavaScript getTimezoneOffset() method is used to find the timezone offset. It returns the timezone difference in minutes, between the UTC and the current local time. If the returned value is positive, local timezone is behind the UTC and if it is negative, the local timezone if ahead of UTC.

How do I find my timezone in my browser?

Open DevTools in Chrome -> Open the Console drawer. Click on the three-dotted menu -> Click on More tools -> Sensors. From the Sensors tab, set the location according to your preference and define the specific timezone.


2 Answers

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.

like image 190
Jose Basilio Avatar answered Sep 25 '22 00:09

Jose Basilio


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&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.

like image 42
Sam152 Avatar answered Sep 25 '22 00:09

Sam152