计算某一年 某一周 的起始时间和结束时间【待修改20180717】

/// <summary>
/// 计算某一年 某一周 的起始时间和结束时间
/// </summary>
/// <param name="year"></param>
/// <param name="week"></param>
/// <param name="first"></param>
/// <param name="last"></param>
/// <returns></returns>
public static bool CalcWeekDay(int year, int week, out DateTime first, out DateTime last)
{
first = DateTime.MinValue;
last = DateTime.MinValue;
//年份超限
if (year < 1700 || year > 9999) return false;
//周数错误
if (week < 1 || week > 53) return false;
//指定年范围
DateTime start = new DateTime(year, 1, 1);
DateTime end = new DateTime(year, 12, 31);
int startWeekDay = (int)start.DayOfWeek;

if (week == 1)
{
first = start;
last = start.AddDays(6 - startWeekDay);
}
else
{
//周的起始日期
first = start.AddDays((7 - startWeekDay) + (week - 2) * 7);
last = first.AddDays(6);
if (last > end)
{
last = end;
}
}
return (first <= end);
}

posted @ 2018-07-06 15:24  BIGBOY_GU  阅读(832)  评论(2编辑  收藏  举报