How to set the local timezone in asp.net application?

Hi,
We have created an application in asp.net(in USA) and we have clients in different parts of the world. The problem is , consider we are in eastern timezone (eg:Georgia) and one of our customers is in Texas, now there is a time variation. We came to know from our texas client that the application is displaying our timezone, but not his.
Is there any way to make the application read/take the time from the local system
[I'll tell abt one situation in our application. In the application(website), theres a 'new customer entry page'. In that, when a new customer comes to the office and signin, his name and the time of his arrival will be saved in the sql DB. The problem is if a cutomer comes there by 9a.m, in the database its saved as 10a.m(which is Eastern time).

Is there anyway to make some setup in web config file or any other solutions ]
Tnx in advance,
Sona


Answer this question

How to set the local timezone in asp.net application?

  • Adriann

  • A Noble

    how do you check the appropriate timezone after you strip it from the string i am having the same porblem converting my time string with PDT to datetime.
  • Milinda V

    Hi!

    It looks like Michael has provided a good answer for you, in future, the best place for asking about Visual Web Developer or ASP.NET is on the ASP.NET community site, and in the forums there. Check out http://www.asp.net/welcome.aspx tabindex=1&tabid=39.

    HTH,

    PEte



  • TopOracle

    Yes this is a difficult issue to deal with. The problem is that timezone information is not directly available through the web browser. You could use heuristics to determine the correct time zone or you will have to store the timezone settings for a user based on their selection.

    Once you have the timezone settings you now need to translate the times. You should always store your date/time values in the DB in UTC. This eliminates many conversion issues. If you store the information in UTC then you need only translate from UTC to the user's local timezone when you display the data to them and you need to convert from their time zone to UTC when you get date/time values from them.

    What makes this more difficult is the fact that the TimeZone class is not all that useful. Fortunately in v2.0 DateTime was updated to support an indicator on whether the time is in UTC or not. You can convert a DateTime to UTC using the DateTime.ToUniversalTime method. Unfortunately however you can't convert it back. The ToLocalTime method uses the local time zone which, when run on the server, uses the server's time zone which isn't what you wanted. Even worse however is that you can't simply create a TimeZone object and use it as that support doesn't exist. Michael Brumm (http://www.michaelbrumm.simpletimezone.html) created a nice little class for being able to create and use time zones easily. I personally use a heavily modified version of that code in my own apps and it works nicely. Here are the steps to convert from a DB UTC value to the local user's time zone.

    1) Get the stored timezone value for the user
    2) Create a SimpleTimeZone class to wrap it (using some mapping scheme that maps a DB value to the underlying Windows registry version)
    3) Use the SimpleTimeZone.ToLocalTime method to convert the DateTime value to the local time.

    For performance reasons you should probably get and initialize the SimpleTimeZone instance and cache it in the Items property for the length of the request so you don't have to keep creating it.

    For converting from the user's local timezone to UTC do the reverse:

    1) Get the stored timezone value from the user
    2) Create a SimpleTimeZone class to wrap it
    3) Use SimpleTimeZone.ToUniversalTime method to convert the DateTime to UTC.

    That's how I've done this sort of thing. Annoying for something that seems so common but it works.

    Michael Taylor - 6/16/06


  • lagroue

    Hello,

    The http://www.michaelbrumm.simpletimezone.html doesn't seem to work any more. Can you get me an updated link or send me the file. I can be emailed at grupe<at>cox.net

    -kidrupe


  • How to set the local timezone in asp.net application?