none
Convert a UTC datetime to localtime in TimeZone considerng daylightsaving

    Question

  • Hi,

    I need to convert the UTC time to localtime of server which is in specific TimeZone. 
    I can retrieve the time zone shift from the server and then use that shift to calculate the localtime of the server from the UTC value. Something like below:

    DateTime dtUtc="2008-10-10 10:00:00";  
    int TimeZoneShift=120; //in mins (TimeZone="Jerusalem GMT +2")  
    DateTime dtLocal=dtUtc.AddMinutes(TimeZoneShift); 


    Now here the problem is that with Daylightsaving time. I am concerned that this will fail when the server is experiencing daylightsaving time. Can anyone help me, how can i convert a UTC value to local time of remote server in a specific TimeZone consiering the daylightsaving time ?

    Thank you




    Kryptonite
    Friday, February 27, 2009 10:02 AM

Answers

  • Hi,

    Have you tried TimeZoneInfo Class?

    Please try the code below which shows wheather a time is a DST time.

    DateTime oldTime1 = new DateTime(2007,12, 23, 10, 0, 0);  
     
    DateTime oldTime2 = new DateTime(2007,12, 23, 10, 0, 0);  
                TimeZoneInfo timeZone1 = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");  
               // TimeZoneInfo timeZone2 = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");  
               // TimeZoneInfo.ConvertTimeFromUtc(  
               //DateTime newTime = TimeZoneInfo.ConvertTime(oldTime, timeZone1, timeZone2);  
               bool isin=timeZone1.IsDaylightSavingTime(oldTime1);  
               isintimeZone1.IsDaylightSavingTime(oldTime2);  
     

    Best regards,
    Harry
    http://cfx.codeplex.com where you can find samples you are looking for
    • Proposed as answer by Harry Zhu Thursday, March 05, 2009 4:18 AM
    • Marked as answer by Sandeep Nakarmi Thursday, March 05, 2009 4:22 AM
    Thursday, March 05, 2009 4:17 AM

All replies

  • Hi,

    We can use TimeZone.GetDaylightChanges Method which returns the daylight saving time period,
    then change the time according to the preriod.

    We can also use TimeZone.ToLocalTime Method which returns the local time that corresponds to a specified date and time value.

    For information ,please take a look at :
    http://msdn.microsoft.com/en-us/library/system.timezone.tolocaltime.aspx

    Best regards,
    Harry
    http://cfx.codeplex.com where you can find samples you are looking for
    Monday, March 02, 2009 8:59 AM
  • Hi Harry,

    thank you for your reply. My problem is that i want to make conversion based on TimeZone of the remote server, i.e, the application may not run in the server system itself. So i cannot use TimeZone.CurrentLocalTime.

    How can i tackle this in such case ?

    Thank you
    Kryptonite
    Monday, March 02, 2009 10:25 AM
  • Hi,

    Have you tried TimeZoneInfo Class which represents any time zone in the world.

    Best regards,
    Harry
    http://cfx.codeplex.com where you can find samples you are looking for
    Tuesday, March 03, 2009 10:40 AM
  • Hi,

    Have you tried TimeZoneInfo Class?

    Please try the code below which shows wheather a time is a DST time.

    DateTime oldTime1 = new DateTime(2007,12, 23, 10, 0, 0);  
     
    DateTime oldTime2 = new DateTime(2007,12, 23, 10, 0, 0);  
                TimeZoneInfo timeZone1 = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");  
               // TimeZoneInfo timeZone2 = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");  
               // TimeZoneInfo.ConvertTimeFromUtc(  
               //DateTime newTime = TimeZoneInfo.ConvertTime(oldTime, timeZone1, timeZone2);  
               bool isin=timeZone1.IsDaylightSavingTime(oldTime1);  
               isintimeZone1.IsDaylightSavingTime(oldTime2);  
     

    Best regards,
    Harry
    http://cfx.codeplex.com where you can find samples you are looking for
    • Proposed as answer by Harry Zhu Thursday, March 05, 2009 4:18 AM
    • Marked as answer by Sandeep Nakarmi Thursday, March 05, 2009 4:22 AM
    Thursday, March 05, 2009 4:17 AM