ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

AddWorkDays

This extension add "working day" to a date, where working day means from Monday to Friday.

Source

public static class DateTimeExtensions
{
    public static DateTime AddWorkDays(this DateTime d, int days)
    {
        for (int i = 0; i < days; ++i)
        {
            if (d.DayOfWeek == DayOfWeek.Saturday || d.DayOfWeek == DayOfWeek.Sunday)
            {
                d = d.AddDays(1.0);
                days++;
                continue;
            }
            
            d = d.AddDays(1.0);
        }
        return d;
    }
}

Example

DateTime dtFrom = DateTime.Parse("05/11/2019");
DateTime dtDelivery = dtFrom.AddWorkDays(5); // 12/11/2019 00:00:00

Author: Jarod Weiss

Submitted on: 7 nov. 2019

Language: csharp

Type: System.DateTime

Views: 6098