C#实现工作日加减(去周末)
public DateTime addWorkDays(DateTime dt,int addDays)
{
DateTime dtStart = dt;
DateTime dtEnd = dtStart.AddDays(-addDays);
int days = CalculateWeekendDays(dtStart,dtEnd);
while(days>0)
{
dtStart = dtEnd;
dtEnd = dtEnd.AddDays(-days);
days = CalculateWeekendDays(dtStart,dtEnd);
}
return dtEnd;
}
public int CalculateWeekendDays(DateTime dtStart, DateTime dtEnd)
{
int count=0;
for(DateTime dtTemp = dtStart;dtTemp > dtEnd;dtTemp = dtTemp.AddDays(-1))
{
if(dtTemp.DayOfWeek==DayOfWeek.Saturday||dtTemp.DayOfWeek==DayOfWeek.Sunday)
{
count++;
}
}
return count;
}
{
DateTime dtStart = dt;
DateTime dtEnd = dtStart.AddDays(-addDays);
int days = CalculateWeekendDays(dtStart,dtEnd);
while(days>0)
{
dtStart = dtEnd;
dtEnd = dtEnd.AddDays(-days);
days = CalculateWeekendDays(dtStart,dtEnd);
}
return dtEnd;
}
public int CalculateWeekendDays(DateTime dtStart, DateTime dtEnd)
{
int count=0;
for(DateTime dtTemp = dtStart;dtTemp > dtEnd;dtTemp = dtTemp.AddDays(-1))
{
if(dtTemp.DayOfWeek==DayOfWeek.Saturday||dtTemp.DayOfWeek==DayOfWeek.Sunday)
{
count++;
}
}
return count;
}