Click here to Skip to main content
Click here to Skip to main content

AddBusinessDay DateTime Extension Method

By , 10 Aug 2007
Rate this:
Please Sign up or sign in to vote.

Introduction

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++)
        {
            do
            {
                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!

History

  • 10th August, 2007: Initial post

License

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

About the Author

martincook

United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
JokeCode Snippet Pinmembermartincook10-Aug-07 4:03 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 10 Aug 2007
Article Copyright 2007 by martincook
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid