locked
TimeZoneInfo RRS feed

  • Question

  • User-1485687286 posted

    The TimeZone Class allows you to get the Start and End dates for Daylight Savings ONLY for the Local TimeZone

    The TimeZoneInfo Class allows you to get the Start and End dates for Daylight Savings for ANY TimeZone. Problem is that it is much more complicated to do so.

    How does one get the Start and End dates for Daylight Savings once a TimeZone has been defined by using the FindSystemTimeZoneById method of the TimeZoneInfo Class?

     

    Sunday, October 18, 2009 8:03 PM

Answers

  • User187056398 posted

     Have you tried code like this yet?

        protected void Button1_Click(object sender, EventArgs e)
        {
            TimeZoneInfo TZI = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
    
            if (TZI.SupportsDaylightSavingTime == true)
            {
                TimeZoneInfo.AdjustmentRule[] Rules = TZI.GetAdjustmentRules();
    
                // note: use the second set of Adjustment rules
                // the first set is before congress changed the rules
                DateTime DS_Date = new DateTime(2010, 1, 1);
    
                int Temp = Rules[1].DaylightTransitionStart.Month;
                DS_Date = DS_Date.AddMonths(Temp-1); // month is already at 1, don't re-add 1
    
                Temp = Rules[1].DaylightTransitionStart.Day;
                DS_Date = DS_Date.AddDays(Temp-1);
    
                Response.Write("2010 Daylight savings starts on: " + DS_Date.ToLongDateString() +"<br />");
    
                // start date over
                DS_Date = new DateTime(2010, 1, 1);
                Temp = Rules[1].DaylightTransitionEnd.Month;
                DS_Date = DS_Date.AddMonths(Temp-1);
               
                Temp = Rules[1].DaylightTransitionEnd.Day;
                DS_Date = DS_Date.AddDays(Temp - 1);
    
                Response.Write("2010 Daylight savings ends on: " + DS_Date.ToLongDateString() + "<br />");
    
            }
    


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 18, 2009 9:42 PM

All replies

  • User187056398 posted

     Have you tried code like this yet?

        protected void Button1_Click(object sender, EventArgs e)
        {
            TimeZoneInfo TZI = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
    
            if (TZI.SupportsDaylightSavingTime == true)
            {
                TimeZoneInfo.AdjustmentRule[] Rules = TZI.GetAdjustmentRules();
    
                // note: use the second set of Adjustment rules
                // the first set is before congress changed the rules
                DateTime DS_Date = new DateTime(2010, 1, 1);
    
                int Temp = Rules[1].DaylightTransitionStart.Month;
                DS_Date = DS_Date.AddMonths(Temp-1); // month is already at 1, don't re-add 1
    
                Temp = Rules[1].DaylightTransitionStart.Day;
                DS_Date = DS_Date.AddDays(Temp-1);
    
                Response.Write("2010 Daylight savings starts on: " + DS_Date.ToLongDateString() +"<br />");
    
                // start date over
                DS_Date = new DateTime(2010, 1, 1);
                Temp = Rules[1].DaylightTransitionEnd.Month;
                DS_Date = DS_Date.AddMonths(Temp-1);
               
                Temp = Rules[1].DaylightTransitionEnd.Day;
                DS_Date = DS_Date.AddDays(Temp - 1);
    
                Response.Write("2010 Daylight savings ends on: " + DS_Date.ToLongDateString() + "<br />");
    
            }
    


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 18, 2009 9:42 PM
  • User-1485687286 posted

     Have you tried code like this yet?

    1. protected void Button1_Click(object sender, EventArgs e)   
    2. {   
    3.     TimeZoneInfo TZI = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");   
    4.   
    5.     if (TZI.SupportsDaylightSavingTime == true)   
    6.     {   
    7.         TimeZoneInfo.AdjustmentRule[] Rules = TZI.GetAdjustmentRules();   
    8.   
    9.         // note: use the second set of Adjustment rules   
    10.         // the first set is before congress changed the rules   
    11.         DateTime DS_Date = new DateTime(2010, 1, 1);   
    12.   
    13.         int Temp = Rules[1].DaylightTransitionStart.Month;   
    14.         DS_Date = DS_Date.AddMonths(Temp-1); // month is already at 1, don't re-add 1   
    15.   
    16.         Temp = Rules[1].DaylightTransitionStart.Day;   
    17.         DS_Date = DS_Date.AddDays(Temp-1);   
    18.   
    19.         Response.Write("2010 Daylight savings starts on: " + DS_Date.ToLongDateString() +"<br />");   
    20.   
    21.         // start date over   
    22.         DS_Date = new DateTime(2010, 1, 1);   
    23.         Temp = Rules[1].DaylightTransitionEnd.Month;   
    24.         DS_Date = DS_Date.AddMonths(Temp-1);   
    25.           
    26.         Temp = Rules[1].DaylightTransitionEnd.Day;   
    27.         DS_Date = DS_Date.AddDays(Temp - 1);   
    28.   
    29.         Response.Write("2010 Daylight savings ends on: " + DS_Date.ToLongDateString() + "<br />");   
    30.   
    31.     }  
        protected void Button1_Click(object sender, EventArgs e)
        {
            TimeZoneInfo TZI = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
    
            if (TZI.SupportsDaylightSavingTime == true)
            {
                TimeZoneInfo.AdjustmentRule[] Rules = TZI.GetAdjustmentRules();
    
                // note: use the second set of Adjustment rules
                // the first set is before congress changed the rules
                DateTime DS_Date = new DateTime(2010, 1, 1);
    
                int Temp = Rules[1].DaylightTransitionStart.Month;
                DS_Date = DS_Date.AddMonths(Temp-1); // month is already at 1, don't re-add 1
    
                Temp = Rules[1].DaylightTransitionStart.Day;
                DS_Date = DS_Date.AddDays(Temp-1);
    
                Response.Write("2010 Daylight savings starts on: " + DS_Date.ToLongDateString() +"<br />");
    
                // start date over
                DS_Date = new DateTime(2010, 1, 1);
                Temp = Rules[1].DaylightTransitionEnd.Month;
                DS_Date = DS_Date.AddMonths(Temp-1);
               
                Temp = Rules[1].DaylightTransitionEnd.Day;
                DS_Date = DS_Date.AddDays(Temp - 1);
    
                Response.Write("2010 Daylight savings ends on: " + DS_Date.ToLongDateString() + "<br />");
    
            }
    


     

     No, I have not, but I will now. Thanks so much for your lightning-fast response and for your time. With best personal regards

    Monday, October 19, 2009 6:18 AM
  • User2054700487 posted

    No, this will not work!

     

    http://msdn.microsoft.com/en-us/library/system.timezoneinfo.transitiontime.isfixeddaterule.aspx

    The Day property is invalid if the IsFixeDateRule is false - which is true at least in the USA.

    Thursday, March 18, 2010 8:26 PM
  • User2054700487 posted

    Here.  I adapted the example on that page to really flush things out properly for reuse.  I couldn't find any closer solution.  I hope this is useful to someone out there!

    -Matt

     

    /// <summary>
    /// Returns the date for time zone transition of a given year
    /// </summary>
    /// <param name="transition">The transition to use</param>
    /// <param name="year">The year to use</param>
    /// <returns>The date the transition falls on</returns>
    public static DateTime GetDateFromTransitionTime(TimeZoneInfo.TransitionTime transition, int year)
    {
        if (transition.IsFixedDateRule)
            return new DateTime(year, transition.Month, transition.Day);
    
        // Get first day of week for transition
        // For example, the 3rd week starts no earlier than the 15th of the month
        var startOfWeek = transition.Week * 7 - 6;
    
        // What day of the week does the month start on?
        var firstDayOfWeek = new DateTime(year, transition.Month, 1).DayOfWeek;
    
        // Determine how much start date has to be adjusted
        var transitionDay = startOfWeek + transition.DayOfWeek - firstDayOfWeek;
        if (firstDayOfWeek > transition.DayOfWeek)
            transitionDay += 7;
    
        // Adjust for months with no fifth week
        if (transitionDay > DateTime.DaysInMonth(year, transition.Month))
            transitionDay -= 7;
    
        var transitionDate = new DateTime(year, transition.Month, transitionDay) + transition.TimeOfDay.TimeOfDay;
    
        return transitionDate;
    }


     

    Thursday, March 18, 2010 8:47 PM