Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: JavaScript
Expertise: Beginner
Jul 10, 2000

Determining a Client's Time Zone

Question:

I want to know from which time zone a client is accessing my site to control display of commercial ads. How can I find this out?

Answer:

In general, these are the steps to determine a client's time zone:

  1. Use JavaScript on the client to determine the time on the client.
  2. Copy the time into an HTML control, such as a textbox, a hidden control or a hyperlink querystring.
  3. You can either wait for the user to click the hyperlink or to submit the form (the one that contains the textbox or hidden control), or you can use JavaScript to force it. Either way, a page request returns to the Web server.
  4. At the server, use server-side processing such as CGI scripts or Active Server Pages to extract the value of the time that is contained in the control mentioned in the previous step.
  5. Use server-side processing to determine the current time on the server.
  6. Compare the time that you have determined for the server with the time that you have determined for the client. Compute the number of hours difference.
  7. Calculate the user's time zone by using the difference calculated above to add to or subtract from your own time zone. For example, suppose my Web server is in Florida (Eastern Time). Suppose that it is 3:00 P.M. in Florida. If a page request comes to my server, and I use the technique outlined above to determine that it is 12:00 P.M. on the client machine, I know that the client's time is 3 hours behind my own. In addition, I know that the user's time zone is 3 hours behind my own. I can easily use logic to determine that 3 time zones behind the Eastern time zone is the Pacific time zone.

The following sample code will help get you started. It will help you determine the time on the client's system and copy that time into an HTML control.