-1

How do you calculate Daylight Savings Time in C# with DateTime.Now? DST starts on the Second Sunday in March. And ends on the first Sunday in November. These can be calculated thru the DayOfWeek in DateTime.

       DayOfWeek dow;
       string p = "3" + "/" + dy.ToString() + "/" + yr.ToString() + " " + "3" + ":" + mn.ToString() + ":" + sc.ToString();
       DateTime start = DateTime.Parse(p);
       p = "11" + "/" + dy.ToString() + "/" + yr.ToString() + " " + "1" + ":" + mn.ToString() + ":" + sc.ToString();
       DateTime end = DateTime.Parse(p);
       DateTime current;
       for (dys = 1; dys <= 17; dys++)
       {
           p = "3" + "/" + dys.ToString() + "/" + yr.ToString() + " " + "3" + ":" + mn.ToString() + ":" + sc.ToString();
           current = DateTime.Parse(p);
           dow = current.DayOfWeek;

           if ((mo == 3) && (aaa == 0) && (dow == DayOfWeek.Sunday))
           {
               aaa = 1;
           }
           if ((aaa == 1) && (dow == DayOfWeek.Sunday))
           {
               start = DateTime.Parse(p);
               aaa = 2;
           }
       }

       for (dye = 1; dye <= 14; dye++)
       {
          p = "11" + "/" + dye.ToString() + "/" + yr.ToString() + " " + "1" + ":" + mn.ToString() + ":" + sc.ToString();
           current = DateTime.Parse(p);
           dow = current.DayOfWeek;

           if ((mo == 11) && (bbb == 0) && (dow == DayOfWeek.Sunday))
           {
                 bbb = 1;
               end = DateTime.Parse(p);
           }
       }
       if ((start >= DateTime.Now) && (end <= DateTime.Now))
       {
           dsts = 0;
       }
       else
       {
           dsts = 1;
       }
Solstice
  • 25
  • 6
  • 3
    Time zones and DST is all handled within the framework - e.g. [`TimeZoneInfo.GetUtcOffset `](https://learn.microsoft.com/en-us/dotnet/api/system.timezoneinfo.getutcoffset?view=net-6.0) or [`TimeZoneInfo.IsDaylightSavingTime`](https://learn.microsoft.com/en-us/dotnet/api/system.timezoneinfo.isdaylightsavingtime?view=net-6.0). I really wouldn't try and reinvent the wheel. What are you actually trying to do? – Charles Mager Apr 03 '22 at 15:36
  • https://stackoverflow.com/questions/10665678/check-if-daylight-savings-is-in-effect – Dmitry Bychenko Apr 03 '22 at 15:36
  • 2
    Please use descriptive variable names in your code. It makes review and maintenance easier both for others *and* for yourself. `aaaa` and `bbbb` are not good variable names. Nor are most of your others. It makes it hard to understand what the *intent* of your code is, which is critical when figuring out why it's not working. – Daniel Mann Apr 03 '22 at 15:50

1 Answers1

0

You can check these microsoft implementations. They already handle timezones and daylight saving time conversions. We do not need to implement them.

You need DateTimeOffset and TimeZoneInfo classes to deal with all these.

Always work with DateTimeOffset class instead of DateTime when dealing with timezones. https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset?view=net-6.0

link1: https://learn.microsoft.com/en-us/dotnet/api/system.timezoneinfo?view=net-6.0

Convert time from one timezone to other https://learn.microsoft.com/en-us/dotnet/api/system.timezoneinfo.converttime?view=net-6.0

Something like below

DateTimeOffset thisTime = DateTimeOffset.Now;
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
bool isDaylight = tzi.IsDaylightSavingTime(thisTime);
DateTimeOffset timeInUtcTimeZone = TimeZoneInfo.ConvertTimeToUtc(thisTime);
DateTimeOffset timeInPstTimeZone = TimeZoneInfo.ConvertTimeToUtc(thisTime, tzi);

Similarly you can convert time from any timezone to any other timezone. And also the comparisons (Equals, greater than, less than) across the timezone will work well and handled by the framework.

Mahesh Bongani
  • 680
  • 7
  • 20