SIGN UP MEMBER LOGIN:    
ARTICLE

Difference between two dates (Excluding weekends)

Posted by Arunava Bhattacharjee Articles | Learn .NET May 25, 2010
Exclude weekends and holidays from a date duration.
Reader Level:

HTML clipboard

Problem:  In projects sometimes it is needed to find the difference between two dates for many important calculations. For example, in typical production support system we need to calculate the SLA( Service Level Agreement) for each people who are assigned a task. It can be project level, account level or individual level. To calculate the SLA timeframe we need to find when the task or ticket has been raised (Start time) and when it is closed (End Time). Now the problem is if a ticket is opened at Friday 6pm and closed on Wednesday 6pm then if you calculate the SLA it will give you wrong data as typically the SLA timeframe should only include the business days. So we need to exclude the weekends from that time difference. Also suppose Monday is a global holiday the sometimes we need to exclude that as well.

Solution:

Suppose we have a List of WeekEnds and Holidays:

List<string> WeekEnds = new List<string>(){"Saturday","Sunday"};

List<DateTime> HolidayList = new List<DateTime>(){All your holidays};

Now calculate the time duration:

resolutionTime=EndDate.Subtract(StartDate);

Now exclude weekends and holidays:

int excludeDates = 0;
for (DateTime temp = resolutionSLAStartTime; temp < resolutionSLAEndTime; temp = temp.AddDays(1))
            {
                foreach (string day in WeekEnds)
                {
                    if (temp.DayOfWeek.ToString() == day)
                    {
                        excludeDates++;
                    }
                }

                foreach (DateTime holiday in HolidayList)
                {
                    if (temp == holiday)
                    {
                        excludeDates++;
                    }
                }

             }

            resolutionTime = resolutionTime.Subtract(new TimeSpan(excludeDates, 0, 0, 0));

Now the resolutionTime will give you the exact result you wanted.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor