Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Application Time Zone Issue

We have a ASP.Net 2.0 web application up and running with the server in the Midwest (Eastern Standard Time). At this moment all of our customers are in the same time zone as the server. We are bringing another server online in Arizona (Mountain Standard Time).

We are storing all our times in a SQL 2005 database via C# codebehind DateTime.UtcNow.

During testing we encountered some time zone conversion issues. Our problem is that in the web browser our times are displaying the Mountain Standard Time instead of the time zone we are testing from which is Eastern Standard Time.

When we enter new information it gets stored as UTC in the database, but when we go to view that info in the browser it is displaying the Mountain Standard Time. Below is the code which takes the UTC value from the database and displays it in the browser.

lblUpdatedDate.Text = Convert.ToDateTime(dr["UpdatedDate"]).ToLocalTime().ToString();

The above code returns Mountain Standard Time where the server is, not Eastern Standard Time where the browser is running from. How do we get the time to display where the user is?

like image 555
Ron Skufca Avatar asked Oct 27 '08 14:10

Ron Skufca


People also ask

How do you handle multiple time zones in your application?

What is the best way to handle multiple time zones in the application ? The user can select the timezone that they are in. The user can be anywhere in the world. When displaying data the time has to be adjusted to the timezone that the user has selected.

How does Java handle time zone issues?

If you cannot change the OS or the JVM timezone, you can still convert a Java Date/Time or Timestamp to a specific time zone using the following two JDBC methods: PreparedStatement#setTimestamp(int parameterIndex, Timestamp x, Calendar cal) – to convert the timestamp that goes to the database.


2 Answers

I had the same issue. We sold our application to a user that was in a different time zone than the web server. We did not store any time information in UTC, but it was actually working correctly. Time displayed in the server's time zone was displaying exactly 3 hours behind. All we had to do was add a time zone drop down so they could select their time zone for the entire site (since the only users of the application would be in their time zone). We saved this setting and then inserted a function to take all datetime displays and convert from one time zone to the other using the TimeZoneInfo namespace. It works perfectly.

like image 65
TheCodeMonk Avatar answered Oct 19 '22 06:10

TheCodeMonk


To local time will always on the server side convert to the physical location. You have a few options.

  1. Store the offset value from UTC to the Users, keep times in UTC
  2. Do the conversion client side via JS (Not elegant, nor appropriate, In my opinion)
  3. Look at some MSDN recommendations and the Timezone namespace
like image 45
Mitchel Sellers Avatar answered Oct 19 '22 07:10

Mitchel Sellers