Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 计算周数,必要时包括第53周_C#_Datetime - Fatal编程技术网

C# 计算周数,必要时包括第53周

C# 计算周数,必要时包括第53周,c#,datetime,C#,Datetime,我正试图找到一个很好的方法,从一个非标准的开始日期计算出周数。第1周应包括4月的第一个星期日。为了计算这一点,我只需循环4月的前7天,直到找到第一个星期天。星期天开始 通常,我会尝试通过以下方式解决此问题: numberOfDaysDifferenceBetweenEpoch / 7 % 52 + 1; 然而,大约每5年就有一次,因为一年有53周。显然,如果恰好是一年53周,上面的函数将不起作用。一个简单的解决方案是只做两个函数,取52或53的模,但是我希望有一个更干净的方法。解决这个问题的最

我正试图找到一个很好的方法,从一个非标准的开始日期计算出周数。第1周应包括4月的第一个星期日。为了计算这一点,我只需循环4月的前7天,直到找到第一个星期天。星期天开始

通常,我会尝试通过以下方式解决此问题:

numberOfDaysDifferenceBetweenEpoch / 7 % 52 + 1;

然而,大约每5年就有一次,因为一年有53周。显然,如果恰好是一年53周,上面的函数将不起作用。一个简单的解决方案是只做两个函数,取52或53的模,但是我希望有一个更干净的方法。解决这个问题的最佳方法是什么

这里有一种方法应该有效。如果您经常使用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;
}
private静态日期时间GetEpochInYear(int-year)
{
DateTime currentYearEpoch=新的日期时间(年,4,1);
while(currentYearEpoch.DayOfWeek!=DayOfWeek.Sunday)
{
currentYearEpoch=currentYearEpoch.AddDays(1);
}
返回当前年份;
}
私有静态int GetWeekNumber(DateTime dateOfInterest)
{
DateTime currentYearEpoch=GetEpochInYear(dateOfInterest.Year);
if(感兴趣的日期<当前年份)
{
currentYearEpoch=GetEpochInYear(感兴趣的日期-1);
}            
整数天=(整数)(感兴趣的日期-当前年份)。总天数;
返回(天/7)+1;
}

这里有一种方法应该有效。如果您经常使用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;
}
private静态日期时间GetEpochInYear(int-year)
{
DateTime currentYearEpoch=新的日期时间(年,4,1);
while(currentYearEpoch.DayOfWeek!=DayOfWeek.Sunday)
{
currentYearEpoch=currentYearEpoch.AddDays(1);
}
返回当前年份;
}
私有静态int GetWeekNumber(DateTime dateOfInterest)
{
DateTime currentYearEpoch=GetEpochInYear(dateOfInterest.Year);
if(感兴趣的日期<当前年份)
{
currentYearEpoch=GetEpochInYear(感兴趣的日期-1);
}            
整数天=(整数)(感兴趣的日期-当前年份)。总天数;
返回(天/7)+1;
}

numberofdaysdifferencebetweenPoch“能有多大?这个变量的最大值是多少?应该检查一下:大于366,所以可以跨越几年,从四月的第一个星期天开始。我不认为我真的了解你想要做的事情。是这样的:您有一个
DateTime
值,想要获得周数,就好像周总是在4月的第一个星期日“重新开始”一样?如果是这样的话,为什么不“简单地”获取价值年4月的第一个星期日,并确定价值是在该日期之前还是之后。如果是在之后,只需从值中减去该日期,然后将天数除以7即可。如果是以前的,则获取前一年4月的第一个星期日,然后从该值中减去该值,然后将天数除以7(…两种情况下都加一个)?@TomDee-如果您想“每次”重新计算,您可以保留一本
字典
,并记住每年4月的第一个星期日可能是一个瓶颈。如果你所有的日期都在一个已知的范围内,你甚至可以预先填写字典。“NumberOfDaysDifferenceBeweenPoch”有多大?这个变量的最大值是多少?应该检查一下:大于366,所以可以跨越几年,从四月的第一个星期天开始。我不认为我真的了解你想要做的事情。是这样的:您有一个
DateTime
值,想要获得周数,就好像周总是在4月的第一个星期日“重新开始”一样?如果是这样的话,为什么不“简单地”获取价值年4月的第一个星期日,并确定价值是在该日期之前还是之后。如果是在之后,只需从值中减去该日期,然后将天数除以7即可。如果是以前的,则获取前一年4月的第一个星期日,然后从该值中减去该值,然后将天数除以7(…两种情况下都加一个)?@TomDee-如果您想“每次”重新计算,您可以保留一本
字典
,并记住每年4月的第一个星期日可能是一个瓶颈。如果你所有的日期都在一个已知的范围内,你甚至可以预先填写字典。