1 回答
TA贡献1853条经验 获得超9个赞
这是一种应该起作用的方法。如果您经常使用GetEpochInYear方法,则可能需要对其进行优化。
private static DateTime GetEpochInYear(int year)
{
DateTime currentYearEpoch = new DateTime(year, 4, 1);
while (currentYearEpoch.DayOfWeek != DayOfWeek.Sunday)
{
currentYearEpoch = currentYearEpoch.AddDays(1);
}
return currentYearEpoch;
}
private static int GetWeekNumber(DateTime dateOfInterest)
{
DateTime currentYearEpoch = GetEpochInYear(dateOfInterest.Year);
if (dateOfInterest < currentYearEpoch)
{
currentYearEpoch = GetEpochInYear(dateOfInterest.Year - 1);
}
int days = (int)(dateOfInterest - currentYearEpoch).TotalDays;
return (days / 7) +1;
}
- 1 回答
- 0 关注
- 157 浏览
添加回答
举报