C#: Adding working days from a cetain date

前端 未结 6 1300
不知归路
不知归路 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:05

    A cool way (i think) is put that in a extension method, like:

    public static class DateTimeExtensions
    {
        public static DateTime AddWorkingDays(this DateTime self, int days)
        {
            self = self.AddDays(days);
            while (self.DayOfWeek == DayOfWeek.Saturday || self.DayOfWeek == DayOfWeek.Sunday)
            {
                self = self.AddDays(1);
            }
    
            return self;
        }
    }
    

    so your final code will look like:

    specificDate.AddWorkingDays(3);
    

提交回复
热议问题