none
Convert DateTime.Now to different time zone

    Question

  • Well, the subject says it all...

     

    How can I convert the current DateTime of the computer I am running into a different time zone time for display purposes, e.g.

    - for a web server where the time zone of a logged in user is known and I want to show events stored in UTC in his local time on the web page

    - for reports: 09:00pm PST, 06:00am GMT, 05:00am CET

     

    I used a couple of fanzy and not so fanzy ways to determine the time zone offset, to even cover the weirdest time zones by getting the entries from the registry and adding or subtracting the offset from the UTC.

     

    But all of the methods which use kind of a fixed offset do not work of course, because of the different switch dates for daylight saving time, so how the heck can I achieve this:

     

    DateTime now = DateTime.Now;

    DateTime utcNow = DateTime.UtcNow;

    // so far.... and beautiful would be this, e.g. with the display name from the registry

    Datetime cetNow = DateTime.ConvertToTimeZone(DateTime.Now, "(GMT+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna");

     

    Which would have last week returned me the local CET with a 10 hour time difference from PST, as the European countries did set their clocks already a week earlier, and this week 9 hours difference.

     

    Wednesday, November 07, 2007 3:09 AM

Answers

  • hi, John.Doe

        As far as I know, I don't think this is possible with the standard libraries of .NET 1.1 and 2.0.

    If you want to do conversion on the server side,you should know the offset between utc and the user time zone and write your own conversion function(There isn't suitable function in standrad class libray to do this). On the other hand, you can do this on the user client side.you can use the JavaScript getTimezoneOffset() to convert times to the local time zone .

        For information about getTimezoneOffset,please visit http://msdn2.microsoft.com/en-us/library/81698774(VS.80).aspx

    Thursday, November 08, 2007 5:45 AM
  • There's nothing built into .Net directly for performing time zone & Daylight saving conversion, however Windows does have registry information about most, if not all current timezones and daylight savings rules.

    Check here for a code example on how to perform timezone conversions. It should be pretty easily applied to what you had in mind:

    http://www.codeproject.com/dotnet/WorldClock.asp

    Thursday, November 08, 2007 6:31 AM
  •  

    Thanks Steve, that example really matches what I want to do.
    Friday, November 09, 2007 4:45 PM

All replies

  • hi, John.Doe

        As far as I know, I don't think this is possible with the standard libraries of .NET 1.1 and 2.0.

    If you want to do conversion on the server side,you should know the offset between utc and the user time zone and write your own conversion function(There isn't suitable function in standrad class libray to do this). On the other hand, you can do this on the user client side.you can use the JavaScript getTimezoneOffset() to convert times to the local time zone .

        For information about getTimezoneOffset,please visit http://msdn2.microsoft.com/en-us/library/81698774(VS.80).aspx

    Thursday, November 08, 2007 5:45 AM
  • There's nothing built into .Net directly for performing time zone & Daylight saving conversion, however Windows does have registry information about most, if not all current timezones and daylight savings rules.

    Check here for a code example on how to perform timezone conversions. It should be pretty easily applied to what you had in mind:

    http://www.codeproject.com/dotnet/WorldClock.asp

    Thursday, November 08, 2007 6:31 AM
  •  

    Thanks Steve, that example really matches what I want to do.
    Friday, November 09, 2007 4:45 PM