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

Business Dates Calculation

, 20 Apr 2005
Rate this:
Please Sign up or sign in to vote.
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

About the Author

Alexander Turlov
Architect
Canada Canada
Alexander Turlov has been working in IT industry since 1987. His programming experience includes such languages as FORTRAN, Pascal, and Basic, C, C++ and C#. He's been working for different industries like science, manufacturing, retail, utilities, finance, insurance, health care, education and so on. His area of interests is rich web applications development with .NET, C#, ASP.NET/MVC and JavaScript. He is working in software development doing architecture, design and development on .NET platform and using Microsoft Visual Studio, IIS and SQL Server as his primary tools, and a lot of other commercial and open source frameworks and tools. He holds a M.Sc. degree in physics and an MCSD.NET certification.

View my profile on LinkedIn

View my blog
Follow on   Twitter

Comments and Discussions

 
QuestionHoliday File PinmemberMember 1045584614-Dec-13 3:00 
Questionthanks for the useful post PinmemberAnubalaji7-Mar-12 16:12 
GeneralMy vote of 5 PinmemberPrashanth-Kh2-Mar-11 6:46 
Questioninternet usage authentication Pinmembershaanu19-Aug-07 23:48 
GeneralProspective Programmer PinmemberAdebisi1-Feb-06 8:28 
GeneralRe: Prospective Programmer PinmemberAlexander Turlov30-Aug-06 17:37 
GeneralGlobalization PinmemberEran Kampf20-Dec-05 21:50 
GeneralRe: Globalization PinmemberAlexander Turlov25-Dec-05 9:38 
GeneralThe guy makes a point! PinmemberAlexander Turlov18-Jun-05 18:36 
GeneralRe: The guy makes a point! PinmemberTrance Junkie4-Aug-05 0:46 
GeneralSomething wrong with your sample PinmemberStefan Schmidt28-Apr-05 2:24 
GeneralRe: Something wrong with your sample PinmemberAlexander Turlov28-Apr-05 13:33 
GeneralRe: Something wrong with your sample PinmemberStefan Schmidt28-Apr-05 20:42 
GeneralRe: Something wrong with your sample PinmemberAlexander Turlov30-Apr-05 5:07 
GeneralWelcome to discussion! PinmemberAlexander Turlov8-Feb-05 6:08 
GeneralRe: Welcome to discussion! Pinmemberbibicool19-Apr-05 21:52 
GeneralRe: Welcome to discussion! PinmemberAlexander Turlov20-Apr-05 15:37 
GeneralRe: Welcome to discussion! PinmemberAlexander Turlov20-Apr-05 16:10 
GeneralRe: Welcome to discussion! PinmemberGarth J Lancaster21-Apr-05 13:38 
GeneralRe: Welcome to discussion! PinmemberAlexander Turlov22-Apr-05 15:56 
GeneralRe: Welcome to discussion! PinmemberGarth J Lancaster22-Apr-05 16:14 
GeneralRe: Welcome to discussion! PinsussPerhentian18-Jul-05 6:04 

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
Web01 | 2.8.140709.1 | Last Updated 20 Apr 2005
Article Copyright 2005 by Alexander Turlov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid