Click here to Skip to main content
15,867,568 members
Articles / Mobile Apps
Article

Business Dates Calculation

Rate me:
Please Sign up or sign in to vote.
4.49/5 (23 votes)
20 Apr 20054 min read 165.9K   2.8K   69   23
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:

C#
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:

C#
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?:

C#
!(_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.

C#
_holidays.ContainsValue(_date.ToString())

Now we can find the next and previous business dates.

Next business date:

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

And previous business date:

C#
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:

C#
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


Written By
Architect
Canada Canada
Alexander Turlov is a professional software development consultant that has been working in IT industry since 1987. His programming experience includes such languages as FORTRAN, Pascal, Basic, C, C++ and C#. He's been working for different industries including but not limited to science, manufacturing, retail, utilities, finance, insurance, health care, education and so on. His area of professional interests is cloud powered 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, Azure and Visual Studio Team Services as his primary tools. He holds a M.Sc. degree in physics and various industry certifications including MCSD.NET, Azure and Scrum.

View my profile on LinkedIn

View my blog

Comments and Discussions

 
QuestionCheer mate Pin
BifdarRhinoVictorHugoSmokeweed14-Aug-17 9:47
BifdarRhinoVictorHugoSmokeweed14-Aug-17 9:47 
QuestionHoliday File Pin
Member 1045584614-Dec-13 3:00
Member 1045584614-Dec-13 3:00 
And how can I change the holidays?
Questionthanks for the useful post Pin
Anubalaji7-Mar-12 16:12
Anubalaji7-Mar-12 16:12 
GeneralMy vote of 5 Pin
Prashanth-Kh2-Mar-11 6:46
Prashanth-Kh2-Mar-11 6:46 
Questioninternet usage authentication Pin
shaanu19-Aug-07 23:48
shaanu19-Aug-07 23:48 
GeneralProspective Programmer Pin
Adebisi1-Feb-06 8:28
Adebisi1-Feb-06 8:28 
GeneralRe: Prospective Programmer Pin
Alexander Turlov30-Aug-06 17:37
Alexander Turlov30-Aug-06 17:37 
GeneralGlobalization Pin
Eran Kampf20-Dec-05 21:50
Eran Kampf20-Dec-05 21:50 
GeneralRe: Globalization Pin
Alexander Turlov25-Dec-05 9:38
Alexander Turlov25-Dec-05 9:38 
GeneralThe guy makes a point! Pin
Alexander Turlov18-Jun-05 18:36
Alexander Turlov18-Jun-05 18:36 
GeneralRe: The guy makes a point! Pin
Trance Junkie4-Aug-05 0:46
Trance Junkie4-Aug-05 0:46 
GeneralSomething wrong with your sample Pin
Stefan Schmidt28-Apr-05 2:24
Stefan Schmidt28-Apr-05 2:24 
GeneralRe: Something wrong with your sample Pin
Alexander Turlov28-Apr-05 13:33
Alexander Turlov28-Apr-05 13:33 
GeneralRe: Something wrong with your sample Pin
Stefan Schmidt28-Apr-05 20:42
Stefan Schmidt28-Apr-05 20:42 
GeneralRe: Something wrong with your sample Pin
Alexander Turlov30-Apr-05 5:07
Alexander Turlov30-Apr-05 5:07 
GeneralWelcome to discussion! Pin
Alexander Turlov8-Feb-05 6:08
Alexander Turlov8-Feb-05 6:08 
GeneralRe: Welcome to discussion! Pin
bibicool19-Apr-05 21:52
bibicool19-Apr-05 21:52 
GeneralRe: Welcome to discussion! Pin
Alexander Turlov20-Apr-05 15:37
Alexander Turlov20-Apr-05 15:37 
GeneralRe: Welcome to discussion! Pin
Alexander Turlov20-Apr-05 16:10
Alexander Turlov20-Apr-05 16:10 
GeneralRe: Welcome to discussion! Pin
Garth J Lancaster21-Apr-05 13:38
professionalGarth J Lancaster21-Apr-05 13:38 
GeneralRe: Welcome to discussion! Pin
Alexander Turlov22-Apr-05 15:56
Alexander Turlov22-Apr-05 15:56 
GeneralRe: Welcome to discussion! Pin
Garth J Lancaster22-Apr-05 16:14
professionalGarth J Lancaster22-Apr-05 16:14 
GeneralRe: Welcome to discussion! Pin
Perhentian18-Jul-05 6:04
Perhentian18-Jul-05 6:04 

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.