Method to find out business hours in between two dates.
public static double CalculateBusinessHours(DateTime dtStart, DateTime dtEnd)
{
int StartingHour = 9;
int EndingHour = 18;
double OverAllMinutes = 0.0;
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);
bool bSameDay = (ctTempStart == ctTempEnd);
int iBusinessDays = GetBusinessDays(ctTempStart, ctTempEnd);
TimeSpan CTimeSpan = new TimeSpan(0, dtStart.Hour, dtStart.Minute, 0);
ctTempStart += CTimeSpan;
CTimeSpan = new TimeSpan(0, dtEnd.Hour, dtEnd.Minute, 0);
ctTempEnd += CTimeSpan;
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);
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;
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.
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)
{
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)
{
return 0;
}
int iStartDay = (Int32)Enum.Parse(typeof(DayOfWeek), ctStart.DayOfWeek.ToString());
if (iStartDay == 1 && iStartDay == 7)
{
return 0;
}
if (ctStart < ctMinTime)
{
ctStart = ctMinTime;
}
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)
{
return 0;
}
int iEndDay = (Int32)Enum.Parse(typeof(DayOfWeek), ctEnd.DayOfWeek.ToString());
if (iEndDay == 1 && iEndDay == 7)
{
return 0;
}
if (ctEnd > ctMaxTime)
{
ctEnd = ctMaxTime;
}
TimeSpan ctSpan = ctEnd - ctMinTime;
daysec = (ctSpan.Days * 24 * 60 * 60) + (ctSpan.Hours * 60 * 60) + (ctSpan.Minutes * 60) + ctSpan.Seconds;
return daysec;
}
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.