views:

67

answers:

3

I have datetime fieldI have datetime field into the database which stores the universal time i.e. UTC time. I want to show the datetime at the client machine in clients time zone and format.

Example: Someone from US updated the database field for a site and it is stored into the UTC format. Someone from India goes and sees the site . What i want is that the person from India sees the time in IST or from Australia sees in his local machines time format not the server time format and zone.

Whats the best way to do this ?? Please paste code snippet if you have.

Thanx in advance!

+1  A: 

If your site has user accounts you can have a setting where they select the relevant time zone, otherwise I think there are ways to get the time zone from the browser, although I think its pretty reliant on Java script

BenW
+1  A: 

Check out this discussion:

How to display the correct time?

Has exactly the answer you're seeking.


Another strategy would be to read the client's IP, map it to the country and location then identify the probable timezone. It may not be precise but it would have the advantage of mostly working even if the machine/browser time/location settings are completely wrong.

Alternatively, just let the user choose his location and store the choice in cookies submitting it with each new request.

Developer Art
+1  A: 

An example in Java if you are happy with that

String utcTimePattern = "yyyy-MM-dd HH:mm:ss Z"; // Defined in database or whatever
String timeString = "2010-09-08 12:51:14 +0000"; // Event time kept in database

SimpleDateFormat timeFormat = new SimpleDateFormat(utcTimePattern);

// Parse server time and get Date object with default settings
Date localTime = timeFormat.parse(timeString);

System.out.println(localTime);

The idea is, you get the UTC time string from server, and format it on the client side using default time zone and date pattern. The output on my machine is

Wed Sep 08 15:51:14 EEST 2010
hudolejev