How to show a UTC time as local time in a webpage?

I have a database that holds a time as UTC. This time can be shown in a webpage, so I’ve been asked to show it as local time in the page as it can be viewed from any country. A colleague mentioned something about getting the country settings from the current thread (on the server) but I couldn’t find any details on this. Is what I want to do possible?

Answers


If you (and your website) are comfortable with javascript, there is a very easy way to accomplish this.

First, on the server side, you would have the UTC date/time formatted in RFC 3339 format (the standard for internet time used by, among other protocols, icalendar). The basic syntax of RFC 3339 is:

YYYY-MM-DDTHH:MM:SS

Such that where I am, the time would be:

2010-05-04T05:52:33

But when the time is not local, but UTC, you add a Z to the end to denote this. So in my case, since I'm at -0500 hours from GMT, the same time above would be:

2010-05-04T10:52:33Z

So, first you get the server to output the above to your web page's javascript. The javascript can then parse that timestamp and it will output the date and time adjusted to the browser's time zone (which is determined by the computer hosting the browser). You should remember that if a user is from Tokyo and is viewing your website in Spain, they will see the timestamp in Tokyo time unless they've adjusted their computer's clock.

So the javascript would be:

    var time_string_utc = some_server_variable; // timestamp from server
    var time_string_utc_epoch = Date.parse(time_string_utc);
    var time_utc = new Date();
    time_utc.setTime(time_string_utc_epoch);

At this point, you have a javascript date object set to your UTC timestamp. A quick explanation of what happens above:

The first variable assumes you have passed the timestamp string to that variable from the server.

The second variable uses the Date.parse() method to convert the string to an epoch timestamp.

The third variable creates the unset Date object.

The last line line uses setTime method, which sets a Date object from an epoch timestamp.

Now that you have the object, you can output it to the user as you see fit. As a simple experiment, you can use:

document.write(time_utc);

which, if you are in my timezone using the UTC timestamp I started off with:

2010-05-04T10:52:33Z

would give:

 Tue May 04 2010 05:52:33 GMT-0500 (CST)

but you can use various javascript methods to format the time into something much more pleasant looking.

No need to guess the user's country or even adjust your timestamp, so long as you trust the user's local browser/computer time zone.

Again, the short version:

    var time_string_utc = some_server_variable; // UTC time from server
    var time_string_utc_epoch = Date.parse(some_server_variable);
    var time_utc = new Date();
    time_utc.setTime(time_string_utc_epoch);
    document.write(time_utc);

Also consider reading this thread which dives deeper in the dos and donts of handling daylight saving problem across timezones


Of-course is possible. You just need to find that country settings to detect the country your user comes from, and then modify the displayed date to fit that country's time.You can also find another way to detect the user country.(maybe from his ip address).

I am sure that the best way to manage this is let your colleague know that you need more details about the country settings implementation on your project and how can you use it.


EDIT:

Thinking about it I don't think it is possible to display the local time once you have the client’s culture. I'd certainly be interested to see your colleagues suggestion.

Getting the US culture, for example, won't help as the US has many timezones.

I think using Javascript like Anthony suggests would be the way to go...

OLD:

You can override the InitializeCulture() method with code that sets the current (chosen or browser reporting) cultures:

Thread.CurrentThread.CurrentCulture = 
            CultureInfo.CreateSpecificCulture(selectedLanguage);
Thread.CurrentThread.CurrentUICulture = new 
            CultureInfo(selectedLanguage);

this is how I've done it in PHP in the login file which connects to MySQL database:

//set local timezone
date_default_timezone_set('America/Denver'); //use local TZ

Then, every time you access the DB, you are setting the timezone for that session and any input/output associated with it.


Need Your Help

Java mapping: Selma vs MapStruct

java mapping mapstruct

Currently there are two main popular Java Object to Object mapping frameworks that supersede Dozer (http://dozer.sourceforge.net/documentation/mappings.html), they are:

AlertDialog styling - how to change style (color) of title, message, etc

android android-alertdialog android-theme android-styles

I've breaking my head over this quite a bit. What I need to do is, change the style of all AlertDialogs in my android application - dialog background needs to be white-ish, and text needs to be bla...