我有Fromdate和Todate。我需要根据工作日(星期六,星期日)将Fromdate和Todate分成列表
Fromdate-11/03/2016
Todate-31/03/2016
型号:
public class Splitdate
{
public DateTime Fromdate { get; set; }
public DateTime Todate { get; set; }
}
我需要输出一个列表:
list[0]=Fromdate:11/03/2016 Todate:11/03/2016
list[1]=Fromdate:14/03/2016 Todate:18/03/2016
list[2]=Fromdate:21/03/2016 Todate:25/03/2016
list[3]=Fromdate:28/03/2016 Todate:31/03/2016
答案 0 :(得分:0)
这应该有效:
DateTime tempStart = StartDate;
var list = new List<Splitdate>();
for (DateTime date = StartDate; date.Date <= EndDate.Date; date = date.AddDays(1))
{
if (day.DayOfWeek == DayOfWeek.Saturday)
{
var temp = new Splitdate();
temp.Fromdate = tempStart;
temp.Todate = date.AddDays(-1);
list.Add(temp);
tempStart.AddDays(2);
}
}
if (tempStart <= EndDate)
{
var temp = new Splitdate();
temp.Fromdate = tempStart;
temp.Todate = EndDate;
list.Add(temp);
}
DateTime具有内置的DayOfWeek属性。循环从Fromdate到Todate的日子,并添加到基于DayOfWeek值的列表。