c#如何获取当前年份的周期及周期所在日期范围

  介绍

这篇文章主要介绍了c#如何获取当前年份的周期及周期所在日期范围,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获、下面让小编带着大家一起了解一下。

最近有一个项目要用到年份周期,用于数据统计图表展示使用,当中用到年份周期,以及年份周期所在的日期范围。当初设想通过已知数据来换算年份周期,经过搜索资料发现通过数据库SQL语句来做,反而更加复杂。现在改变思路通过c#后台代码来算出两段日期范围中年份周期,在依据年份周期所对应的日期范围进行数据库查询进行统计。需要解决以下两个点问题,

第一点:依据日期查找所在年份的第几周;

第二点:依据年份所在的周期计算出周期所在的日期范围。

using 系统;   using  System.Collections.Generic;   using  System.Globalization;   using 来;   using 包含;   using  System.Threading.Tasks;   namespace  ConsoleApplication6   {   class 才能;程序   {才能   ,,,static  void  Main (string [], args)   ,,,{   ,,,,,GregorianCalendar  gc =, new  GregorianCalendar ();   ,,,,,int  weekOfYear =, gc.GetWeekOfYear (DateTime.Now, CalendarWeekRule.FirstDay,, DayOfWeek.Monday);   ,,,,,Console.WriteLine(“当前第{0}周“,,weekOfYear);   ,,,,,DateTime  startDate可以,,lastDate;   ,,,,,for  (int 小姐:=,1;,小姐:& lt;=, 53;,我+ +)   ,,,,,{   ,,,,,,,GetDaysOfWeeks (DateTime.Now.Year,,我,,out  startDate可以,,out  lastDate);   ,,,,,,,Console.WriteLine(“第{0}周“,,i);   ,,,,,,,Console.WriteLine (startDate可以);   ,,,,,,,Console.WriteLine (lastDate);   ,,,,,}   ,,,,,Console.ReadLine ();   ,,,}   ,,,public  static  bool  GetDaysOfWeeks (int ,, int 指数,out  DateTime 首先,out  DateTime 最后)   ,,,{   ,,,,,first =, DateTime.MinValue;   ,,,,,last =, DateTime.MinValue;   ,,,,,if  (year  & lt;, 1700, | |, year 祝辞,9999)   ,,,,,{   ,,,,,,,//澳攴莩蕖?   ,,,,,,,return 假;   ,,,,,}   ,,,,,if  (index  & lt;, 1, | |, index 祝辞,53)   ,,,,,{   ,,,,,,,//爸苁砦蟆?   ,,,,,,,return 假;   ,,,,,}   ,,,,,DateTime  startDay =, new  DateTime(,, 1, 1),,//该年第一天   ,,,,,DateTime  endDay =, new  DateTime (year  +, - 1, 1, 1) .AddMilliseconds (1);   ,,,,,int  dayOfWeek =, 0;   ,,,,,if  (Convert.ToInt32 (startDay.DayOfWeek.ToString (“d")),祝辞,0)   ,,,,,,,dayOfWeek =, Convert.ToInt32 (startDay.DayOfWeek.ToString (“d")),,//该年第一天为星期几   ,,,,,if  (dayOfWeek ==, 0), {, dayOfWeek =, 7,,}   ,,,,,if  (==index  1)   ,,,,,{   ,,,,,,,first =, startDay.AddDays(7,安康;dayOfWeek 作用;6);   ,,,,,,,if  (==dayOfWeek  6)   ,,,,,,,{   ,,,,,,,,,last =,第一;   ,,,,,,,}   ,,,,,,,   ,,,,,,,{   ,,,,,,,,,last =, startDay.AddDays((7,安康;dayOfWeek));   ,,,,,,,}   ,,,,,}   ,,,,,   ,,,,,{   ,,,,,,,first =, startDay.AddDays((8,安康;dayOfWeek), +, (index 安康;2),*,7);,//指数周的起始日期   ,,,,,,,last =, first.AddDays (6);   ,,,,,,,//if  (last 祝辞,endDay)   ,,,,,,,//{   ,,,,,,,//,,last =, endDay;   ,,,,,,,//}   ,,,,,}   null   null   null   null   null   null   null   null   null

c#如何获取当前年份的周期及周期所在日期范围