C# 计算夏令时

发布于 2025-01-18 18:37:09 字数 1550 浏览 0 评论 0原文

您如何使用datetime.now计算C#中的日光节省时间? DST从第二个开始 三月的星期日。并在11月的第一个星期日结束。这些可以通过DateTime的Dayofweek计算。

  Dayofweek Dow;
       字符串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电流;
       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)
           {
               AAA = 1;
           }
           if(((aaa == 1)&&(dow == dayofweek.sunday))
           {
               start = datetime.parse(p);
               AAA = 2;
           }
       }

       for(染料= 1; dye< = 14;染料++)
       {
          p =“ 11” +“/” + dye.toString() +“/” + yr.tostring() +“” +“” +“ 1” +“:” + mn.tostring() +“:” + sc.tostring ();
           current = datetime.parse(p);
           Dow = Current.dayofweek;

           if(((mo == 11)&&(bbb == 0)
           {
                 BBB = 1;
               end = datetime.parse(p);
           }
       }
       if(((start> = datetime.now)&&(end< = dateTime.now))
       {
           DSTS = 0;
       }
       别的
       {
           DSTS = 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;
       }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终难愈 2025-01-25 18:37:09

您可以检查这些微软的实现。他们已经处理时区和夏令时转换。我们不需要实施它们。

您需要 DateTimeOffset 和 TimeZoneInfo 类来处理所有这些。

处理时区时,始终使用 DateTimeOffset 类而不是 DateTime。 https://learn.microsoft.com/ en-us/dotnet/api/system.datetimeoffset?view=net-6.0

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

将时间从一个时区转换为其他时区 https://learn.microsoft.com/ en-us/dotnet/api/system.timezoneinfo.converttime?view=net-6.0

如下所示同样

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);

,您可以将时间从任何时区转换为任何其他时区。而且跨时区的比较(等于、大于、小于)也将很好地工作并由框架处理。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文