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;
}
原文:https://www.cnblogs.com/gaolijun1986/p/15017514.html