您的位置:首页 > 其它

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

2018-07-06 15:24 357 查看

/// <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);
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐