/// <summary> /// 根据时间段获取每星期X的总数 /// </summary> /// <param name="beginDateTime">开始时间</param> /// <param name="endDateTime">结束时间</param> /// <param name="week">星期几 0:星期日</param> /// <returns></returns> public static int GetWeekCount(DateTime beginDateTime, DateTime endDateTime, List<DayOfWeek> weekList) { } 如何让这个单元测试相等呀? List<DayOfWeek> list = new List<DayOfWeek>(); list.Add(DayOfWeek.Tuesday); list.Add(DayOfWeek.Wednesday);//从2012-5-1日到2012-5-31 日有10天符合星期二或星期三 TestHelper.Test(DateTimeHelper.GetWeekCount(DateTime.Parse("2012-5-1"), DateTime.Parse("2012-5-31"), list) == 10);
2 回答
德玛西亚99
TA贡献1770条经验 获得超3个赞
public static int GetWeekCount(DateTime begin, DateTime end, List<DayOfWeek> weekList) { return Enumerable.Range(0, (end - begin).Days) .Count(n => weekList.Contains(begin.AddDays(n).DayOfWeek)); }
智慧大石
TA贡献1946条经验 获得超3个赞
获取一段时间内有多少个周几,你这样写试试呢:
public static int GetWeekCount(DateTime beginDateTime, DateTime endDateTime, List<DayOfWeek> weekList)
{
int num = 0;
for (; beginDateTime < endDateTime; beginDateTime = beginDateTime.AddDays(1))
{
foreach ( DayOfWeek dow in weekList)
{
if (beginDateTime.DayOfWeek == dow)
num++;
}
}
return num;
}
- 2 回答
- 0 关注
- 919 浏览
添加回答
举报
0/150
提交
取消