|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionIn many cases developers have a demand of calculation and manipulations with business dates. Standard tasks are usually the following:
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.
PurposeCommonly speaking, Microsoft .NET Framework Library already contains the Here comes the purpose of the Basic ApproachBasically, the Primary CalculationsAs 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 Meet XDateTimeAt last it's time to take a closer look at the Member Variables
public enum XDateTimeType { Calendar=0, Business=1 };
public class XDateTime
{
private DateTime _date;
private XDateTimeType _type;
private Hashtable _holidays;
...
}
Here I define an enumeration ConstructorsFor convenience, I include three constructors for class instantiation: public XDateTime();
public XDateTime(string dateTime);
public XDateTime(string dateTime, XDateTimeType dateType);
Main LogicFirst 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 _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. ConclusionIn 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
|
||||||||||||||||||||||