1. 不允许跨年
1) 第一周的第一天从每年的第一天开始,最后一周的最后一天为每年的最后一天。
static void Main(string[] args) { DateTime first, last; , , , }; , , }; foreach (int y in years) { foreach (int w in weeks) { bool result = CalcWeekDay(y, w, out first, out last); Console.WriteLine("{0}第{1}周({2:yyyy-MM-dd} ~ {3:yyyy-MM-dd}) --{4}", y, w, first, last, result); } Console.WriteLine(); } } public static bool CalcWeekDay(int year, int week, out DateTime first, out DateTime last) { first = DateTime.MinValue; last = DateTime.MinValue; //年份超限 || year > ) return false; //周数错误 || week > ) return false; //指定年范围 DateTime start = , ); DateTime end = , ); int startWeekDay = (int)start.DayOfWeek; ) { first = start; last = start.AddDays( - startWeekDay); } else { //周的起始日期 first = start.AddDays(( - startWeekDay) + (week - ) * ); last = first.AddDays(); if (last > end) { last = end; } } return (first <= end);
2) 程序执行结果
2. 允许跨年
1) 每年的尾周剩余天数计入下一年第一周。
public static bool CalcWeekDay(int year, int week, out DateTime first, out DateTime last) { first = DateTime.MinValue; last = DateTime.MinValue; //年份超限 || year > ) return false; //周数错误 || week > ) return false; //指定年范围 DateTime start = , ); DateTime end = , ); int startWeekDay = (int)start.DayOfWeek; //周的起始日期 first = start.AddDays(( - startWeekDay) + (week - ) * ); last = first.AddDays(); //结束日期跨年 return (last <= end); }
2) 程序执行结果