C#: Adding working days from a cetain date

前端 未结 6 1298
不知归路
不知归路 2021-02-14 18:10

I have trouble doing this. I\'m creating a method that add working days on a specific date. for example, I want to add 3 working days to sept 15, 2010 (Wednesday), the method wo

6条回答
  •  遥遥无期
    2021-02-14 19:06

    This seems to me the cleanest way:

    public static DateTime AddWorkingDays(DateTime date, int daysToAdd)
    {
        while (daysToAdd > 0)
        {
            date = date.AddDays(1);
    
            if (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday) daysToAdd -= 1;
        }
    
        return date;
    }
    

提交回复
热议问题