高利军

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

public int GetWeeksCountBetweenTime(DateTime dtBegin, DateTime dtEnd)
{
if (dtEnd <= dtBegin)
{
throw new InvalidOperationException("结束时间要大于开始时间!");
}
double totalDays = (dtEnd - dtBegin).TotalDays + 1;
double weeks = 0;
if (dtBegin.DayOfWeek == DayOfWeek.Monday)
{
weeks = Math.Ceiling(totalDays / 7);
}
else
{
if (dtBegin.DayOfWeek == DayOfWeek.Sunday)
{
totalDays -= 1;
}
else
{
totalDays -= 7 - ((int)dtBegin.DayOfWeek - 1);
}
weeks = Math.Ceiling(totalDays / 7) + 1;
}
//如果算某天是某段时间内的第几周,实际上就是算以这个时间作为结束时间的周数,直接调用该函数,dtEnd传入某天的值即可。
return (int)weeks;
}

posted on 2021-07-15 21:01  高利军  阅读(242)  评论(0编辑  收藏  举报