5

I would like to be able to display time based on the user's current time zone. I will be storing times in UTC format in my database, but wish to normalize these to the client's time zone. So far the option that seems most viable is to capture the clients time and then deteremine the difference between that and current UTC and use that delta to normalize times when rendering. I would like to know if there are any more straight forward options available to detect and normalize a UTC time to that of the requesting client's machine.

2
  • How do you know the client has their time set accurately? You can't win. Mar 23, 2009 at 1:21
  • I considered that... but I'll consider it a win if I can just base it on their time (right or wrong) :). Mar 23, 2009 at 1:24

3 Answers 3

8

I would use the javascript dateObject.getTimezoneOffset(). Even if their time isn't set accurately, hopefully they've set their timezone:

http://www.w3schools.com/jsref/jsref_getTimezoneOffset.asp

You can use this value to calculate an offset between the server's time and the client's time. (as Jason helpfully pointed out in a comment below)

1
  • Your solution is right. I had to solve this problem for our web app about 6 months ago and came to the same conclusion: use the server time corrected by the client time zone. Mar 23, 2009 at 1:29
2

In addition to, or instead of using getTimezoneOffset, you should consider permitting the user to specify preferred timezone in their profile. For instance, a user may be visiting in one timezone, yet may prefer to see time displayed in his home timezone.

Also, if this were as simple as calling a JavaScript function, then sites would never have to ask for your timezone.

0

A much better approach is to store per-user the exact time zone the user is in (and optionally) allow them to select if they observe daylight savings time (if it's applicable to their time zone.) You can of course, automatically fill in the DST information (based on the time zone) if you wanted.

You'd have to make this part of the sign-up or possibly in their "options".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.