來自:http://blog.csdn.net/vrhero/archive/2008/01/13/2042481.aspx
/// <summary>
/// 擷取一年中指定的一周的開始日期和結束日期。開始日期遵循ISO 8601即星期一。
/// </summary>
/// <remarks>Write by vrhero</remarks>
/// <param name="year">年(1 到 9999)</param>
/// <param name="weeks">周(1 到 53)</param>
/// <param name="weekrule">確定首周的規則</param>
/// <param name="first">當此方法返回時,則包含參數 year 和 weeks 指定的周的開始日期的 System.DateTime 值;如果失敗,則為 System.DateTime.MinValue。如果參數 year 或 weeks 超出有效範圍,則操作失敗。該參數未經初始化即被傳遞。</param>
/// <param name="last">當此方法返回時,則包含參數 year 和 weeks 指定的周的結束日期的 System.DateTime 值;如果失敗,則為 System.DateTime.MinValue。如果參數 year 或 weeks 超出有效範圍,則操作失敗。該參數未經初始化即被傳遞。</param>
/// <returns>成功返回 true,否則為 false。</returns>
public static bool GetDaysOfWeeks(int year, int weeks, CalendarWeekRule weekrule, out DateTime first, out DateTime last)
{
//初始化 out 參數
first = DateTime.MinValue;
last = DateTime.MinValue;
//不用解釋了吧...
if (year < 1 | year > 9999)
return false;
//一年最多53周地球人都知道...
if (weeks < 1 | weeks > 53)
return false;
//取當年首日為基準...為什嗎?容易得唄...
DateTime firstCurr = new DateTime(year, 1, 1);
//取下一年首日用於計算...
DateTime firstNext = new DateTime(year + 1, 1, 1);
//將當年首日星期幾轉換為數字...星期日特別處理...ISO 8601 標準...
int dayOfWeekFirst = (int)firstCurr.DayOfWeek;
if (dayOfWeekFirst == 0) dayOfWeekFirst = 7;
//得到未經驗證的周首日...
first = firstCurr.AddDays((weeks - 1) * 7 - dayOfWeekFirst + 1);
//周首日是上一年日期的情況...
if (first.Year < year)
{
switch (weekrule)
{
case CalendarWeekRule.FirstDay:
//不用解釋了吧...
first = firstCurr;
break;
case CalendarWeekRule.FirstFullWeek:
//順延一周...
first = first.AddDays(7);
break;
case CalendarWeekRule.FirstFourDayWeek:
//周首日距年首日不足4天則順延一周...
if (firstCurr.Subtract(first).Days > 3)
{
first = first.AddDays(7);
}
break;
default:
break;
}
}
//得到未經驗證的周末日...
last = first.AddDays(7).AddSeconds(-1);
//周末日是下一年日期的情況...
if (last.Year > year)
{
switch (weekrule)
{
case CalendarWeekRule.FirstDay:
last = firstNext.AddSeconds(-1);
break;
case CalendarWeekRule.FirstFullWeek:
//不用處理
break;
case CalendarWeekRule.FirstFourDayWeek:
//周末日距下一年首日不足4天則提前一周...
if (firstNext.Subtract(first).Days < 4)
{
first = first.AddDays(-7);
last = last.AddDays(-7);
}
break;
default:
break;
}
}
return true;
}