Method to get the no.of working days between two dates excluding weekends


Method to get the no.of working days between two dates excluding weekends

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

About sharepointsriram

9+ Yrs of IT experience
This entry was posted in Sharepoint 2010 Server Object Model. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s