Click here to Skip to main content
13,350,483 members (70,351 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 10 Aug 2007

AddBusinessDay DateTime Extension Method

, 10 Aug 2007
Rate this:
Please Sign up or sign in to vote.
An extension method that extends the AddDay method to include only business days


I needed a generic solution to resolve our international delivery needs and as we are working on business days, we needed a way to take this into account. Inheriting from the DateTime class seemed the first port of call, but the DateTime class is sealed. So the best solution was to use an extension method to get around this problem (only available in C# 3.0 onwards?). This is a simple solution that only includes Saturday and Sunday as "holiday" days... please feel free to extend this further to include your countries national holidays/bank holidays, etc. My inspiration for this was from the following CodeProject article which I found by Googling: Business Dates C#.

US Holiday Days Web Service

There is actually a Web service available that exposes US holiday days but we (my company) are going to implement a different solution embedded within our existing architecture.

Using the Code

Dead easy! Put this wherever you like and it will extend the DateTime class (System.DateTime) to add the following method - AddBusinessDays(int days):

public static class DateTimeExtensions
    public static DateTime AddBusinessDays(this DateTime date, int days)
        double sign = Convert.ToDouble(Math.Sign(days));
        int unsignedDays = Math.Sign(days) * days;
        for (int i = 0; i < unsignedDays; i++)
                date = date.AddDays(sign);
            while (date.DayOfWeek == DayOfWeek.Saturday || 
                date.DayOfWeek == DayOfWeek.Sunday);
        return date;

The extension method works in exactly the same way as the standard AddDays method.

If you came here to learn about extension methods, then you can see how ridiculously easy it is. Simply define a static class and call it whatever you like, then include a static method that contains this as the first parameter, i.e. DoWhateverYouWant(this type ref) so if you want to write an extension method for the DataTable class you could simply do:

public static void DoWhateverYouWant(this DataTable myDataTable) { ... }

My intention was not to teach about extension methods - if this is your bag, then check out this article.

Extending the Extension Method (Don't You Just Love .NET)

Like I said, please feel free to extend this to include national holidays, localization and whatever else you can come up with!


  • 10th August, 2007: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United Kingdom United Kingdom
No Biography provided

You may also be interested in...


Comments and Discussions

JokeCode Snippet Pin
martincook10-Aug-07 5:03
membermartincook10-Aug-07 5:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 10 Aug 2007
Article Copyright 2007 by martincook
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid