none
How to get Daylight Saving Time of different Time Zones?

    Question

  • This line will give Daylight Saving time for current machine:

    double daylightValue = Convert.ToDouble(System.TimeZone.CurrentTimeZone.GetDaylightChanges(DateTime.Now.Year));

    How do I get DaylightChanges value for different time zone?

    Or how to get datetime of different time zone with daylightchanges? I will input the timezone (like Pacific Standard Time). And output I want datetime with daylight time.

    Thanks,

    Jai

    Friday, June 04, 2010 3:50 PM

Answers

  • This worked for me. The results were matching.
    // Define times to be converted.
        DateTime timeToConvert = new DateTime(DateTime.Now.Year, 
                          DateTime.Now.Month, 
                          DateTime.Now.Day, 
                          DateTime.Now.Hour, 
                          DateTime.Now.Minute, 
                          DateTime.Now.Second);
    
        // Retrieve the time zone for Eastern Standard Time (U.S. and Canada).
        TimeZoneInfo est1;
    
        try
        {
          est1 = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
        }
        catch (TimeZoneNotFoundException)
        {
          Console.WriteLine("Unable to retrieve the time zone.");
          return;
        }
        catch (InvalidTimeZoneException)
        {
          Console.WriteLine("InvalidTimeZoneException :Unable to retrieve the time zone.");
          return;
        }
    
        // Convert localtime to respective time zone time.
        DateTime targetTime = TimeZoneInfo.ConvertTime(timeToConvert, est1);
    
    Monday, June 07, 2010 5:49 AM

All replies

  • You can use below code to convert datetime to different timezone with daylight savings.

    DateTime hwTime = new DateTime(2007, 02, 01, 08, 00, 00);
    try
    {
      TimeZoneInfo hwZone = TimeZoneInfo.FindSystemTimeZoneById("Hawaiian Standard Time");
      Console.WriteLine("{0} {1} is {2} local time.", 
          hwTime, 
          hwZone.IsDaylightSavingTime(hwTime) ? hwZone.DaylightName : hwZone.StandardName, 
          TimeZoneInfo.ConvertTime(hwTime, hwZone, TimeZoneInfo.Local));
    }
    catch (TimeZoneNotFoundException)
    {
      Console.WriteLine("The registry does not define the Hawaiian Standard Time zone.");
    }              
    catch (InvalidTimeZoneException)
    {
      Console.WriteLine("Registry data on the Hawaiian STandard Time zone has been corrupted.");
    }
    


    Nayan Paregi (MCTS)
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Proposed as answer by Nayan Paregi Monday, June 07, 2010 5:53 AM
    Friday, June 04, 2010 4:28 PM
  • Thank you

    I tried what you said. Here is the extension of your code:

    static

     

    void Main(string[] args)

    {

     

    DateTime hwTime = new DateTime(DateTime.Now.Year,

     

    DateTime.Now.Month,

     

    DateTime.Now.Day,

     

    DateTime.Now.Hour,

     

    DateTime.Now.Minute,

     

    DateTime.Now.Second);

     

    try

    {

     

    TimeZoneInfo hwZone = TimeZoneInfo.FindSystemTimeZoneById("Hawaiian Standard Time");

     

    Console.WriteLine("{0} {1} is {2} local time.",

    hwTime,

    hwZone.IsDaylightSavingTime(hwTime) ? hwZone.DaylightName : hwZone.StandardName,

     

    TimeZoneInfo.ConvertTime(hwTime, hwZone, TimeZoneInfo.Local));

     

    TimeZoneInfo hwZone1 = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");

     

    Console.WriteLine("{0} {1} is {2} local time.",

    hwTime,

    hwZone1.IsDaylightSavingTime(hwTime) ? hwZone1.DaylightName : hwZone1.StandardName,

     

    TimeZoneInfo.ConvertTime(hwTime, hwZone1, TimeZoneInfo.Local));

     

    TimeZoneInfo hwZone2 = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");

     

    Console.WriteLine("{0} {1} is {2} local time.",

    hwTime,

    hwZone2.IsDaylightSavingTime(hwTime) ? hwZone2.DaylightName : hwZone2.StandardName,

     

    TimeZoneInfo.ConvertTime(hwTime, hwZone2, TimeZoneInfo.Local));

     

    TimeZoneInfo hwZone3 = TimeZoneInfo.FindSystemTimeZoneById("Mountain Standard Time");

     

    Console.WriteLine("{0} {1} is {2} local time.",

    hwTime,

    hwZone3.IsDaylightSavingTime(hwTime) ? hwZone3.DaylightName : hwZone3.StandardName,

     

    TimeZoneInfo.ConvertTime(hwTime, hwZone3, TimeZoneInfo.Local));

     

    TimeZoneInfo hwZone4 = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");

     

    Console.WriteLine("{0} {1} is {2} local time.",

    hwTime,

    hwZone4.IsDaylightSavingTime(hwTime) ? hwZone4.DaylightName : hwZone4.StandardName,

     

    TimeZoneInfo.ConvertTime(hwTime, hwZone4, TimeZoneInfo.Local));

     

    }

     

    catch (TimeZoneNotFoundException)

    {

    }

     

    catch (InvalidTimeZoneException)

    {

    }

     

    Console.Read();

     

    But none of the time obtained matched with the actual time.

    For example, the code returned:

    6/5/2010 12:51:51 PM Hawaiian Standard Time is 6/6/2010 4:21:51 AM local time.
    6/5/2010 12:51:51 PM Pacific Daylight Time is 6/6/2010 1:21:51 AM local time.
    6/5/2010 12:51:51 PM Central Daylight Time is 6/5/2010 11:21:51 PM local time.
    6/5/2010 12:51:51 PM Mountain Daylight Time is 6/6/2010 12:21:51 AM local time.
    6/5/2010 12:51:51 PM Eastern Daylight Time is 6/5/2010 10:21:51 PM local time.

    When I check Eastern Time in computer ( right bottom corner), the time it shows is 3.25 PM!!!!!!

    More help needed.

    Saturday, June 05, 2010 7:22 AM
  • I have checked on my machine and it is working fine

     My Current System Time is 2:33 (IST) and EST Time is 3:03 which is correct as per Daylight saving. As daylight saving informations are stored in registry so might be machine on which you are running does not contain update information. You can read more details about same problem "here"


    Regards
    Nayan Paregi (MCTS)
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Saturday, June 05, 2010 9:04 AM
  • This problem is, for all practical purposes, unsolvable. Daylight saving time is not consistantly observed.

    For computer programs, we generally don't care about DST in locations other than at the machine the code is currently executing on, in which case the clock, time zone, and DST observance are part of the locale.

    When you store date/time somewhere (e.g. database) you should store UTC time which doesn't take DST (or timezone) into account. When you retreive a UTC date/time and display it to a human, you can convert it to Local time.

    Saturday, June 05, 2010 2:46 PM
  • This worked for me. The results were matching.
    // Define times to be converted.
        DateTime timeToConvert = new DateTime(DateTime.Now.Year, 
                          DateTime.Now.Month, 
                          DateTime.Now.Day, 
                          DateTime.Now.Hour, 
                          DateTime.Now.Minute, 
                          DateTime.Now.Second);
    
        // Retrieve the time zone for Eastern Standard Time (U.S. and Canada).
        TimeZoneInfo est1;
    
        try
        {
          est1 = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
        }
        catch (TimeZoneNotFoundException)
        {
          Console.WriteLine("Unable to retrieve the time zone.");
          return;
        }
        catch (InvalidTimeZoneException)
        {
          Console.WriteLine("InvalidTimeZoneException :Unable to retrieve the time zone.");
          return;
        }
    
        // Convert localtime to respective time zone time.
        DateTime targetTime = TimeZoneInfo.ConvertTime(timeToConvert, est1);
    
    Monday, June 07, 2010 5:49 AM