Click here to Skip to main content
15,881,741 members
Articles / Programming Languages / C#
Tip/Trick

Calculate Business Hours

Rate me:
Please Sign up or sign in to vote.
4.54/5 (9 votes)
23 Feb 2010CPOL 29.9K   14   4
Method to find out business hours in between two dates. public static double CalculateBusinessHours(DateTime dtStart, DateTime dtEnd) { int StartingHour = 9; int EndingHour = 18; // initialze our return value double OverAllMinutes = 0.0; ...
Method to find out business hours in between two dates.

C#
public static double CalculateBusinessHours(DateTime dtStart, DateTime dtEnd)
   {
       int StartingHour = 9;
       int EndingHour = 18;

       // initialze our return value
       double OverAllMinutes = 0.0;

       // start time must be less than end time
       if (dtStart > dtEnd)
       {
           return OverAllMinutes;
       }
       DateTime ctTempEnd = new DateTime(dtEnd.Year, dtEnd.Month, dtEnd.Day, 0, 0, 0);
       DateTime ctTempStart = new DateTime(dtStart.Year, dtStart.Month, dtStart.Day, 0, 0, 0);

       // check if startdate and enddate are the same day
       bool bSameDay = (ctTempStart == ctTempEnd);

       // calculate the business days between the dates
       int iBusinessDays = GetBusinessDays(ctTempStart, ctTempEnd);

       // now add the time values to our temp times
       TimeSpan CTimeSpan = new TimeSpan(0, dtStart.Hour, dtStart.Minute, 0);
       ctTempStart += CTimeSpan;
       CTimeSpan = new TimeSpan(0, dtEnd.Hour, dtEnd.Minute, 0);
       ctTempEnd += CTimeSpan;

       // set our workingday time range and correct the first day
       DateTime ctMaxTime = new DateTime(ctTempStart.Year, ctTempStart.Month, ctTempStart.Day, EndingHour, 0, 0);
       DateTime ctMinTime = new DateTime(ctTempStart.Year, ctTempStart.Month, ctTempStart.Day, StartingHour, 0, 0);
       Int32 FirstDaySec = CorrectFirstDayTime(ctTempStart, ctMaxTime, ctMinTime);

       // set our workingday time range and correct the last day
       DateTime ctMaxTime1 = new DateTime(ctTempEnd.Year, ctTempEnd.Month, ctTempEnd.Day, EndingHour, 0, 0);
       DateTime ctMinTime1 = new DateTime(ctTempEnd.Year, ctTempEnd.Month, ctTempEnd.Day, StartingHour, 0, 0);
       Int32 LastDaySec = CorrectLastDayTime(ctTempEnd, ctMaxTime1, ctMinTime1);
       Int32 OverAllSec = 0;

       // now sum-up all values
       if (bSameDay)
       {
           if (iBusinessDays != 0)
           {
               TimeSpan cts = ctMaxTime - ctMinTime;
               Int32 dwBusinessDaySeconds = (cts.Days * 24 * 60 * 60) + (cts.Hours * 60 * 60) + (cts.Minutes * 60) + cts.Seconds;
               OverAllSec = FirstDaySec + LastDaySec - dwBusinessDaySeconds;
           }
       }
       else
       {
           if (iBusinessDays > 1)
               OverAllSec =
               ((iBusinessDays - 2) * 9 * 60 * 60) + FirstDaySec + LastDaySec;
       }
       OverAllMinutes = OverAllSec / 60;

       return OverAllMinutes / 60;


   }


This method uses following functions.

C#
public static DateTime AddBusinessDays(DateTime dt, int nDays)
   {
       int weeks = nDays / 5;
       nDays %= 5;
       while (dt.DayOfWeek == DayOfWeek.Saturday || dt.DayOfWeek == DayOfWeek.Sunday)
           dt = dt.AddDays(1);

       while (nDays-- > 0)
       {
           dt = dt.AddDays(1);
           if (dt.DayOfWeek == DayOfWeek.Saturday)
           {
               dt = dt.AddDays(2);
           }
       }
       return dt.AddDays(weeks * 7);
   }

   private static int GetBusinessDays(DateTime ctStart, DateTime ctEnd)
   {
       TimeSpan ctp = ctEnd - ctStart;
       int iDays = ctp.Days + 1;
       int iWeeks = iDays / 7;
       int iBusDays = iWeeks * 5;
       int iRem = iDays % 7;
       while (iRem > 0)
       {
           // no sunday, no saturday
           int iStartDay = (Int32)Enum.Parse(typeof(DayOfWeek), ctStart.DayOfWeek.ToString());
           if (iStartDay != 1 && iStartDay != 7)
           {
               iBusDays++;
           }
           TimeSpan time1 = new TimeSpan(1, 0, 0, 0);
           ctStart += time1;

           iRem--;
       }
       return iBusDays;
   }

   private static Int32 CorrectFirstDayTime(DateTime ctStart, DateTime ctMaxTime, DateTime ctMinTime)
   {
       Int32 daysec = 0;

       if (ctMaxTime < ctStart) // start time is after max time
       {
           return 0; // zero seconds for the first day
       }
       int iStartDay = (Int32)Enum.Parse(typeof(DayOfWeek), ctStart.DayOfWeek.ToString());
       if (iStartDay == 1 && iStartDay == 7)
       {
           return 0;
       }
       if (ctStart < ctMinTime) // start time is befor min time
       {
           ctStart = ctMinTime; // set start time to min time
       }
       TimeSpan ctSpan = ctMaxTime - ctStart;
       daysec = (ctSpan.Days * 24 * 60 * 60) + (ctSpan.Hours * 60 * 60) + (ctSpan.Minutes * 60) + ctSpan.Seconds;
       return daysec;
   }
   private static Int32 CorrectLastDayTime(DateTime ctEnd, DateTime ctMaxTime, DateTime ctMinTime)
   {
       Int32 daysec = 0;

       if (ctMinTime > ctEnd) // start time is after max time
       {
           return 0; // zero seconds for the first day
       }
       int iEndDay = (Int32)Enum.Parse(typeof(DayOfWeek), ctEnd.DayOfWeek.ToString());
       if (iEndDay == 1 && iEndDay == 7)
       {
           return 0;
       }
       if (ctEnd > ctMaxTime) // start time is befor min time
       {
           ctEnd = ctMaxTime; // set start time to min time
       }
       TimeSpan ctSpan = ctEnd - ctMinTime;
       daysec = (ctSpan.Days * 24 * 60 * 60) + (ctSpan.Hours * 60 * 60) + (ctSpan.Minutes * 60) + ctSpan.Seconds;
       return daysec;
   }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Samsung India Electronics Pvt. Ltd.
India India
Gaurav Dudeja has done B-Tech from ABES Engg College, Ghaziabad, Uttar Pradesh, India . He is an interested, committed, creative Software professional having more than 3 years of solid experience in web-based and windows based solutions in Microsoft Technologies using .NET 2.0, .NET 3.0 , .NET 3.5, ASP.NET 2.0, ASP.NET 3.5 C# 2.0, AJAX, Web Services, MS SQL Server 2005, WSS (Windows Sharepoint Server 3.0 ). He is also an MCP (Microsoft Certified Professional) and MCTS (Microsoft Certified Technology Specialist) on Web Development (.NET 2.0 ). He has good knowledge of Object Oriented Programming, 3-Tier Architecture and Design Patterns as well as good command over IIS (IIS 5.1,IIS 6.0, IIS 7.0) and deployment of Application on Live Production Environment.

Comments and Discussions

 
GeneralThis is perle1's algorithm converted to C# Pin
u166229-Dec-10 6:59
u166229-Dec-10 6:59 

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.