Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Business Dates Calculation

0.00/5 (No votes)
20 Apr 2005 1  
This document demonstrates a simple way of business dates calculation using standard features of the .NET Framework Library.

Introduction

In many cases developers have a demand of calculation and manipulations with business dates. Standard tasks are usually the following:

  • Calculate next business date from current date.
  • Calculate previous business date from current date.
  • Calculate a date in future or in past that is distant from current date by several business days.
  • Check if current date is a working day or holiday and so on.

Formally, business dates set is a subset of calendar dates that doesn�t include such dates as weekend days and holidays. Business dates and business periods are often used for defining dates concerning to business agreements, so the term of Business Date is very famous and widely utilized.

Therefore it seems to be very useful having a standard way to manipulate business dates.

XDateTime C# class described in this article is an applied class that provides a basic functionality for business date manipulations and calculations.

Purpose

Commonly speaking, Microsoft .NET Framework Library already contains the DateTime class that provides quite a lot functionality for manipulating dates. But, as already mentioned, there is no functionality for calculation of business dates. Thus, the basic idea of XDateTime class was not to repeat what we already have in the standard DateTime but to expand its features to cover business date operations.

Here comes the purpose of the XDateTime. Use the XDateTime class in cases where you require additional functionality with business dates manipulation than that provided by the  standard System.DateTime type from the Microsoft .NET Framework. XDateTime class doesn�t repeat any functionality that already exists in the System.DateTime type. So, there is no reason to use the XDateTime instead of the System.DateTime. Use them together to extend existing features of the System.DateTime type.

Basic Approach

Basically, the XDateTime is a wrapper for the standard DateTime. XDateTime contains an instance of the DateTime type as a private class member variable and performs almost all the operations using this private variable. All the functionality of working with business dates is exposed as several public methods. Also some properties are provided for utility purpose.

Primary Calculations

As a matter of fact, we only need to perform two basic calculations: calculate the next business date from the given date, and the previous business date. In order to calculate an arbitrary business date from the given date, we can just repeat one of the mentioned basic calculations as many times as needed.

In that way calculating business dates actually means moving consequently along the time axis forth or back with a step of a single day, skipping weekends and holidays.

Hence, the corner stone in the business date calculation is of knowing exactly the holidays that should be excluded from the calendar dates set. Thus XDateTime contains a special method that initiates a list of holidays used for calculations.

Meet XDateTime

At last it's time to take a closer look at the XDateTime class itself. You can get the entire XDateTime code from the zipped Visual Studio .NET solution provided above. Here, let's consider only the key parts of the XDateTime implementation that prove the basic assumptions.

Member Variables

XDateTime contains several private member variables used for calculations:

public enum XDateTimeType { Calendar=0, Business=1 };

public class XDateTime
{
   private DateTime _date;
   private XDateTimeType _type;
   private Hashtable _holidays;
   ...
}

Here I define an enumeration XDateTimeType and use a private _date member of the standard DateTime type. The standard Hashtable is used for storing a holiday list.

Constructors

For convenience, I include three constructors for class instantiation:

public XDateTime();
public XDateTime(string dateTime);
public XDateTime(string dateTime, XDateTimeType dateType);

Main Logic

First of all, we have to determine whether the current date is a business day. As we already mentioned business dates don�t include weekends and holidays. So, the result of the following logic expression is an answer to the question Is a Business Day?:

!(_date.DayOfWeek == DayOfWeek.Saturday ||
        _date.DayOfWeek == DayOfWeek.Sunday || this.IsHoliday)

where AddDays and DayOfWeek are standard features of the DateTime class, and IsHoliday is defined using a standard method of the Hashtable.

_holidays.ContainsValue(_date.ToString())

Now we can find the next and previous business dates.

Next business date:

do 
{
   date = date.AddDays(1.0);
}
while (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday || 
       _holidays.ContainsValue(date.ToString(_format)));

And previous business date:

do 
{
   date = date.AddDays(-1.0);
}
while (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday || 
      _holidays.ContainsValue(date.ToString(_format)));

As you can see, I repeat adding or subtracting a day until it's not a business date according to my definition. Actually that's it. The only detail else I want to mention is it's very useful to define a helper private method of checking the current date against the business date definition and use this method every time I change a date. We can do it like the following:

private void check()
{
   if (_type == XDateTimeType.Business && !this.IsWorkDay)
   {
      _date = this.NextBusinessDay();
   }
}

It is very important to notice that I slide the date over only forward, not backward. That's pretty obvious; if you change a date, say add five days, and after changing, the resulting date is not a business date, the date should be implicitly changed to the next business date.

Conclusion

In this article, I have shortly described a simple approach of business date calculation. I have to mention that this approach has been used in a real development project and showed the total accuracy of it. In real life you may want to adapt the XDateTime class for your requirements, like reading a holiday list from a database or something else. You can do it easily using the source code provided. And please feel free to discuss any questions and suggestions you might have made with me.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here