C#获取某年某个星期的第一天和最后一天的日期

 1 /// <summary>
 2 /// 获取某年第几周的第一天和最后一天的日期
 3 /// </summary>
 4 /// <param name="year">年份</param>
 5 /// <param name="weekNumber">第几周</param>
 6 /// <returns></returns>
 7 public Tuple<DateTime, DateTime> GetFirstEndDayOfWeek(int year, int weekNumber)
 8 {
 9 System.Globalization.Calendar calendar = System.Globalization.CultureInfo.CurrentCulture.Calendar;
10 DateTime firstOfYear = new DateTime(year, 1, 1, calendar);//获取这一年的第一天日期
11 DateTime targetDay = calendar.AddWeeks(firstOfYear, weekNumber - 1);//加上weekNumber - 1 周的日期
12 DayOfWeek firstDayOfWeek = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;//星期一:Monday
13 //获取第weekNumber周第一天的日期
14 while (targetDay.DayOfWeek != firstDayOfWeek)
15 {
16 targetDay = targetDay.AddDays(-1);
17 }
18 return Tuple.Create<DateTime, DateTime>(targetDay, targetDay.AddDays(6));
19 }
 1 /// <summary>
 2         /// 计算时间差
 3         /// </summary>
 4         /// <param name="dateBegin">开始时间</param>
 5         /// <param name="dateEnd">结束时间</param>
 6         /// <returns>返回(天)单位,比如: 1天</returns>
 7         private int ExecDateDiff(DateTime dateBegin, DateTime dateEnd)
 8         {
 9             TimeSpan ts1 = new TimeSpan(dateBegin.Ticks);
10             TimeSpan ts2 = new TimeSpan(dateEnd.Ticks);
11             TimeSpan ts3 = ts1.Subtract(ts2).Duration();
12             //你想转的格式
13             return ts3.Days;
14         }

 

posted on 2021-09-29 15:18  写abcd的猴子  阅读(274)  评论(0编辑  收藏  举报