我正在尝试计算一个日期范围内每周的天数。我的一周是坐着。 我有一个开始日期和一个持续时间(和一个周数 - 实际上我想要返回整个范围内特定周的天数)
编辑:我会尝试更清楚一点。我确实希望在下一个星期六之前的日子跨越一个日期范围..所以是的,当它的范围足够长时,第二周将永远是7天。然而,如果持续时间没有延长到星期六,那么最后一周将是持续时间的剩余天数。希望下面的答案会减少更多的光......但也许不是。 周数是整个日期范围内的一周。与一年中的几周下降无关。 值得注意,到达日可以是星期六,所以如果它的星期六开始和7个持续时间需要返回7 ...
所以基本上如果我说
arrivalDate = 29/06/2014 (sunday)
weeknumber = 1
duration = 17
我需要它返回6
如果周数为2,则应返回7
如果周数为3,则应返回4
到目前为止我有这个,我知道这远非理想 - 期待一些递归可能效果最好,但我的大脑还没有完全运作..
public static int DaysInWeek(DateTime arrivalDate, int weekNumber, int duration)
{
int ret =0;
List<int> arr = new List<int>();
int leftOver = 0;
for (int i = 1; i <= duration; i++)
{
if (arrivalDate.AddDays(i).DayOfWeek == DayOfWeek.Saturday)
{
int x = i;
arr.Add( x - arr.Sum() );
leftOver = duration - arr.Sum();
}
}
if (leftOver < 7 && leftOver > 0) arr.Add( leftOver );
if (arr.Count == 0) ret = duration;
else ret = arr[weekNumber - 1];
return ret;
}
感谢
答案 0 :(得分:1)
这应该做你想要的。
public static int DaysInWeek(DateTime arrivalDate, int weekNumber, int duration)
{
const int daysInAWeek = 7;
//get the value of Saturday (your week start day)
int firstDayOfWeekIndex = (int)DayOfWeek.Saturday;
//get the day of week of the first day
int startDay = arrivalDate.DayOfWeek;
//Find out number of says until next Saturday (days in first week)
int daysInFirstWeek = (startDay + firstDayOfWeekIndex) % daysInAWeek;
//Get the 'Full Weeks', that have all 7 days in the duration
int fullWeeks = (duration - daysInFirstWeek) / daysInAWeek;
//Get any leftover days
int leftover = duration - daysInAWeek * fullWeeks - daysInFirstWeek;
//Get total number of weeks (complete or otherwise)
int totalWeeks = 1 + fullWeeks + (leftover > 0 ? 1 : 0);
//return accordingly
if(weekNumber > totalWeeks)
return 0;
else if(weekNumber == 1)
return daysInFirstWeek;
else if(weekNumber == totalWeeks)
return daysInLastWeek;
else return daysInAWeek;
}
没有列表,没有迭代,也没有需要递归。
希望这会有所帮助。欢呼声。
答案 1 :(得分:1)
以下是基于CeejeeB解决方案的解决方案,该解决方案将星期六作为第一天和星期数来处理超出持续时间定义的时间跨度。
public static int DaysInWeek(DateTime arrivalDate, int weekNumber, int duration)
{
// Handle a Saturday as start day
var daysInFirstWeek = arrivalDate.DayOfWeek == DayOfWeek.Saturday ? 7 : DayOfWeek.Saturday - arrivalDate.DayOfWeek;
// First week
if (weekNumber == 1) return Math.Min(duration, daysInFirstWeek);
// Other week
var start = daysInFirstWeek + ((weekNumber - 2) * 7);
return Math.Max(0, Math.Min(7, duration - start));
}
答案 2 :(得分:0)
这是一个较短的版本,可以满足您的3个测试用例。不需要递归。基本上,结果将落入3个结果中的1个。结果总是要么是第一周,整个中间周或上周的一部分。 此代码涉及三种情形。
public static int DaysInWeekNew(DateTime arrivalDate, int weekNumber, int duration)
{
var offset = arrivalDate.DayOfWeek == DayOfWeek.Saturday ? 7 : DayOfWeek.Saturday - arrivalDate.DayOfWeek;
//First Week
if (duration < offset) return duration;
if (weekNumber == 1) return offset;
//Middle Week
var start = offset + ((weekNumber - 2) * 7);
if (start + 7 < duration) return 7;
//Last Week
return duration - start;
}
根据评论更新
答案 3 :(得分:0)
我的解决方案:
public static int DaysInWeekNew(DateTime arrivalDate, int weekNumber, int duration)
{
var offset = DayOfWeek.Saturday - arrivalDate.DayOfWeek;
//First Week
if (offset == 0) offset = 7; //if startdays is saturday
if (weekNumber == 1) return offset > duration ? duration : offset;
int numberofmiddleweeks = (duration - offset) / 7; //floor
//middleweeks
if (weekNumber - 1 <= numberofmiddleweeks) return 7;//-1 for the first week
//Last Week
return duration - offset - numberofmiddleweeks * 7;
}
编辑:星期六开始更新