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;
        }
posted @ 2012-06-07 10:35  great wang  阅读(458)  评论(1编辑  收藏  举报