12,826,440 members (23,560 online)
alternative version

#### Stats

18.6K views
13 bookmarked
Posted 8 Feb 2010

, 23 Feb 2010 CPOL
 Rate this:
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.

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

// 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)
{
{
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
{
OverAllSec =
((iBusinessDays - 2) * 9 * 60 * 60) + FirstDaySec + LastDaySec;
}
OverAllMinutes = OverAllSec / 60;

return OverAllMinutes / 60;

}

This method uses following functions.

{
int weeks = nDays / 5;
nDays %= 5;
while (dt.DayOfWeek == DayOfWeek.Saturday || dt.DayOfWeek == DayOfWeek.Sunday)

while (nDays-- > 0)
{
if (dt.DayOfWeek == DayOfWeek.Saturday)
{
}
}
}

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;
}

## Share

 Technical Lead Samsung India Electronics Pvt. Ltd. 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.

## You may also be interested in...

 Pro

 First Prev Next
 Wrong Result Member 958023221-Feb-15 11:03 Member 9580232 21-Feb-15 11:03
 My vote of 4 paulitocat31-Oct-12 6:42 paulitocat 31-Oct-12 6:42
 This is perle1's algorithm converted to C# u166229-Dec-10 7:59 u16622 9-Dec-10 7:59
 This appears to be perle1's algorithm, converted to C#. You need to give credit to the original author!http://www.codeproject.com/KB/datetime/CalculatingBusinessHours.aspx
 Just a question The Manoj Kumar16-Mar-10 15:22 The Manoj Kumar 16-Mar-10 15:22
 Last Visit: 31-Dec-99 19:00     Last Update: 28-Mar-17 15:02 Refresh 1