Search code examples
timezoneutc

Setting and displaying event time to the client at different time zones


I was wondering what is the best way to deal with time zones.

Let's say user1 wrote a message to user2

Now user1 is at UTC + 2 and user2 is at UTC -2 and let's say server sets time in UTC +1

So how would i deal with all this difference in time zones?

I am writing jquery plugin which is going to update time how long ago something happened, my best guess is to set server to UTC 0 and then just calculate time difference based on client time zone, but is there a way to get client time zone, or maybe im doing it in a wrong way?


Solution

  • Use epoch time (UTC+00) from top to bottom and ignore time zones. Assume browser/OS time zone is correct. This means every message sent and received has a timestamp in the form of number of milliseconds since epoch, generated using new Date().getTime().

    My current time is:

    Thu Oct 18 2012 22:07:11 GMT+0200 (CEST)
    

    or:

    1350590831507
    

    CEST is UTC+02. The latter form interpreted on target computer (new Date(1350590831507) with UTC-0200) will be:

    Thu Oct 18 2012 18:07:11 GMT-0200
    

    But it's still the same date (point in time)! If ten seconds later you compare this timestamp with new Date().getTime(), you will always get 10000 ms, irrespective to time zone.