Timezone map (from netschool.de)Most web applications in this World 2.0 target international users distributed among many different timezones. It is pretty simple to ask the users what their timezone is when they registered to your site. This article aims to solve the case of unregistered users who you can’t ask in a usable way. The solution isn’t perfect, but it’s better than nothing.

In theory and technologically, we could determine what timezone the user is in by:

  • Using a geo location database to guess their longitude and latitude, and then find the probable timezone from it ;
  • Using JavaScript to read the user’s workstation clock and calculate the probable timezone from it. You can see a live demo here.

Geo location, while pretty simple nowadays with free databases floating around, leaves us with a problem: the free databases are often not good enough to pin point a precise longitude and latitude, which will cause garbage in and garbage out when trying to translate it to a timezone. Moreover, the non-free and precise databases are pretty expensive compared to the added value of timezone detection, and are most likely not worth buying just for this purpose.

JavaScript clock reading on its side offers a pretty simple yet functional solution. The two downsides are:

  • It is impossible to detect the timezone for users who disabled JavaScript (a very few in the AJAX world).
  • The precision depends on the exactness of the users’ computer clock settings, as its the piece of information we base our calculation on.

Here are the three macroscopic steps for the detection:

  • Try to detect the user’s timezone through JavaScript, then send it via an XML-HTTP call to a script that will save it either as a cookie or as a session. Alternatively, you could use the JavaScript setcookie function.
  • If detection is impossible (JavaScript is disabled), try to read the previously detected timezone in a cookie.
  • If no timezone can be detected, use a default one (or chain in the geolocation method).

The detection code itself is:

function setTimeZone() {
 	var xmlhttp;
 	timezone = "EDT";	/* Initialize httpRequest object */
 	if (window.ActiveXObject) {
 	 	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 	} else if (window.XMLHttpRequest) {
 	 	xmlhttp = new XMLHttpRequest();
 	xmlhttp.open("POST", "/timezone-controller.php",true);
 	xmlhttp.onreadystatechange=function() {
 	 	if (xmlhttp.readyState==4) {
 	 	 	// Do nothing

 	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 	var visitortime = new Date();
} setTimeZone();

I personally prefer to use the epoch value rather than the timezoneoffset one, as, in my experience, local timezone settings are more often incorrectly set than local time.

In conclusion, it is possible to detect your users’ timezone in a fairly simple way using JavaScript, so you can present them dates and times relative to their location, without having to ask them.

However, the method being imperfect, the safest way to determine their exact timezone will always be to ask them. Thus, the implementation of this method shouldn’t replace the parameter in their profile.

World timezone map found on netschool.de.
Article previously published in French at Quantik Solutions.
Code distributed under the LGPL license.

If you like this article, leaving a comment, tweeting ofr liking it is always appreciated.

category HTML, Usability Monday 25 February 2008 Comments (2)

2 Responses :

  1. Partha


    I wanted to fetch the client’s time zone name like ‘Indian Standard Time’ or IST.

    Firefox browser will return this timezone name but not IE.

    Through offset value we can able to manipulate the timezone name, since more than one timezone has similar offset value I cannot find exact time zone name.

    If your code does that pls send me the full sample source code.


  2. steps wooden

    I became looking for this particular information in a long time. Warm regards and good luck.

Leave a Reply