views:

655

answers:

3

I'm working on a site that lets users add calendar entries, on the main page all users can then see these entries with some text saying how long until those entries are current for example

Entry 1 : 5 Minutes
Entry 2 : 7 Hours
Entry 3 : 4 Days

The problem I'm having is that everyone could be in different timezones. I've decided I want to store all times/dates in GMT.

What I'm sturggling with is when a user submits a calendar entry say for 1/1/2009 10:30AM I then need to work out what this date time is in GMT for storage. Is there a way of knowing what the users local timezone/time is so I can compare it to server time (GMT) to make the conversion?

I really hope this makes sense. I'm hoping I can do this without having to ask the user what timezone they are in, or forcing the user to enter all datetimes in GMT.

+3  A: 

COnvert the datetime to GMT client side (in JavaScript if this is a web app) before sending it to the server. Let the server only work with GMT (I would say UTC). Same thing for displaying dates/time. Send GMT/UTC to client, and let JS localize. This is the only completely safe way in my opinion, because you let it be up to the users operating system to figure out what the time is supposed to be.

There are other approaches of cause, and if you only need to know what the client's time is right now you can just get the offset from it (can be retrieved through JS etc). But if you need accurate dates/times in different locations for historical and future date/times you better let the client handle it.

PS: In my TimeZone implementation I also use UNIX time format for communication between client and server. This is just plain easier to parse, and I belive it's the native datetime format for JS. I can't present my code for you now, but should be relatively easy to google for this.

Torbjørn
+1  A: 

Your only option (besides explicit user choice) is to obtain the time zone offset from the browser:

var tzOffset = new Date().getTimezoneOffset(); // Minutes

Then you need to send it to the server by using a hidden field or other suitable approach.

This will give you only a time zone offset, not a complete time zone information. This isn't very reliable, so I would still leave the choice to the user, but use this information to preselect a suitable time zone.

Tsvetomir Tsonev
+1  A: 

Incase anyones interested I found a site that gives a good working example of this in action

http://www.tellingmachine.com/post/2008/05/Rendering-ASPNET-UTC-time-as-local-time-of-the-browsers-time-zone.aspx

Gavin Draper
Nice article, though a lot more code than I've used to enable the same thing (by using UNIX time).
Torbjørn