Click here to Skip to main content
Licence 
First Posted 4 Feb 2005
Views 95,242
Downloads 1,060
Bookmarked 63 times

Business Dates Calculation

By Alexander Turlov | 20 Apr 2005
This document demonstrates a simple way of business dates calculation using standard features of the .NET Framework Library.
2 votes, 9.1%
1

2
1 vote, 4.5%
3
8 votes, 36.4%
4
11 votes, 50.0%
5
4.45/5 - 22 votes
2 removed
μ 3.95, σa 2.09 [?]

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

Member

Follow on Twitter Follow on Twitter
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 and JavaScript. He is working in software development doing architecture, design and development on .NET platform and using Microsoft products such as Visual Studio, SQL Server, P&P Software Factories, Enterprise Library and some other products like BizTalk and SharePoint. He holds a M.Sc. degree in physics and an MCSD.NET certification.

View my profile on LinkedIn

View my blog

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberPrashanth-Kh7:46 2 Mar '11  
Questioninternet usage authentication Pinmembershaanu0:48 20 Aug '07  
GeneralProspective Programmer PinmemberAdebisi9:28 1 Feb '06  
GeneralRe: Prospective Programmer PinmemberAlexander Turlov18:37 30 Aug '06  
GeneralGlobalization PinmemberEran Kampf22:50 20 Dec '05  
GeneralRe: Globalization PinmemberAlexander Turlov10:38 25 Dec '05  
GeneralThe guy makes a point! PinmemberAlexander Turlov19:36 18 Jun '05  
GeneralRe: The guy makes a point! PinmemberTrance Junkie1:46 4 Aug '05  
GeneralSomething wrong with your sample PinmemberStefan Schmidt3:24 28 Apr '05  
GeneralRe: Something wrong with your sample PinmemberAlexander Turlov14:33 28 Apr '05  
GeneralRe: Something wrong with your sample PinmemberStefan Schmidt21:42 28 Apr '05  
GeneralRe: Something wrong with your sample PinmemberAlexander Turlov6:07 30 Apr '05  
GeneralWelcome to discussion! PinmemberAlexander Turlov7:08 8 Feb '05  
GeneralRe: Welcome to discussion! Pinmemberbibicool22:52 19 Apr '05  
GeneralRe: Welcome to discussion! PinmemberAlexander Turlov16:37 20 Apr '05  
GeneralRe: Welcome to discussion! PinmemberAlexander Turlov17:10 20 Apr '05  
GeneralRe: Welcome to discussion! PinmemberGarth J Lancaster14:38 21 Apr '05  
Hi Alexander - good article
 
do you think there are any mathematical ways of doing the same versus this iterative method ?? it occurs to me that if the # days was larger than (maybe 7, ie a week), a pure mathematical method could be formulated (although everything Ive seen does iterate) ..
 
my thinking goes alone the lines of :-
 
lets assume we start on Wednesday 6th April, and Im asked to calulate 14 business days from then (Im going to cheat and only go 'forward' for this example)
 
a rough calulation would go
 
6th April (as Julian or 'linear' date) - note, there's a number of assumptions here !
plus 14 days requested
plus 2 days for every weekend
plus any holidays
 
ok,
 
so we have 96 (day # since the start of the year, just for a quick test)
plus 14 days requested
plus ((14 div 7 (integer arithmetic)) = # Weekends) * 2 (=4 calendar days)
plus 1 holiday (April 25th is a public holiday here)
 
which would end up on 115 (April 25th, which is the holiday) - but since we started with 96 being April 6th, in reality I think you'd start from 'the next day', ie the 7th or '97' and then you'd end up on 116 - April 26th, which is more what I had thought if I simply traced it on a calendar
 
It points out that one would have to be careful about the start and end of a business day (hence once of the assumptions mentioned in the rough method) and what you mean when you says '14 business days from today' for example - do you mean to start counting from the end of the current business day or what ? what happens if you start on a Saturday for example ??
 
I have seen long discussions about when you start the 14 day period from, there doesnt appear to be a standard ...
 
maybe one could do something like :-
 
Julian date (Today)
(plus 2 if Today = Saturday, 1 if Today = Sunday, or n public holidays)
plus 14 days requested
plus 2 days for every weekend
plus any holidays
 
it seems the iterative method solves some of the issues with the caluclatiuon method, at the expense of looping for potentially a large number of iterations !!
 
anyway, thanks for your article ...
 
Garth
GeneralRe: Welcome to discussion! PinmemberAlexander Turlov16:56 22 Apr '05  
GeneralRe: Welcome to discussion! PinmemberGarth J Lancaster17:14 22 Apr '05  
GeneralRe: Welcome to discussion! PinsussPerhentian7:04 18 Jul '05  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 20 Apr 2005
Article Copyright 2005 by Alexander Turlov
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid