public static int CalculateBusinessDays(DateTime startDate, DateTime endDate)
{
//Check if Start Date is greater than end date, return -1
if (startDate > endDate)
return -1;
DateTime dateTemp;
int weekDays = 0;
int weekEnds = 0;
TimeSpan tsDiff = endDate.Subtract(startDate);
//Calculate number of weekend days between two dates
for (int i = 0; i < tsDiff.Days; i++)
{
dateTemp = startDate.AddDays(i + 1);//starting from next day of starting date
if (dateTemp.DayOfWeek == DayOfWeek.Saturday || dateTemp.DayOfWeek == DayOfWeek.Sunday)
weekEnds++;
}
weekDays = tsDiff.Days - weekEnds;
return weekDays;
}
{
//Check if Start Date is greater than end date, return -1
if (startDate > endDate)
return -1;
DateTime dateTemp;
int weekDays = 0;
int weekEnds = 0;
TimeSpan tsDiff = endDate.Subtract(startDate);
//Calculate number of weekend days between two dates
for (int i = 0; i < tsDiff.Days; i++)
{
dateTemp = startDate.AddDays(i + 1);//starting from next day of starting date
if (dateTemp.DayOfWeek == DayOfWeek.Saturday || dateTemp.DayOfWeek == DayOfWeek.Sunday)
weekEnds++;
}
weekDays = tsDiff.Days - weekEnds;
return weekDays;
}
Comments
Post a Comment