查询当前日期是当年第几周,整型数
分享C#获取当前日期是当年的第几周的方法。
好用就好。
1 /// <summary>
2 /// 查询当前日期是当年第几周,整型数
3 /// </summary>
4 /// <param name="currentDate">当前日期</param>
5 /// <returns>Integer类型</returns>
6 public static int GetCurrentWeek(DateTime currentDate)
7 {
8 try
9 {
10 DateTime firstDate = new DateTime(currentDate.Year, 1, 1);
11 int firstWeek = Convert.ToInt32(firstDate.DayOfWeek); 12 firstWeek = firstWeek.Equals(0) ? 7 : firstWeek;
13
14 int currentWeek = Convert.ToInt32(currentDate.DayOfWeek);
15 currentWeek = currentWeek.Equals(0) ? 7 : currentWeek;
16 int currentDays = Convert.ToInt32(currentDate.DayOfYear);
17
18 int n = (currentDays - 8 + firstWeek - currentWeek) / 7 + 2;
19
20 return n;
21 }
22 catch (Exception)
23 {
24 return 0;
25 }
26 }
27