Thursday 25 August 2016

Calculate the duration of the visit based on the Start and End Date. The duration will only count regular business days (does not count weekends or holidays)

private double GetBusinessDays(DateTime startD, DateTime endD)
{
   double calcBusinessDays =
           1 + ((endD - startD).TotalDays * 5 -
               (startD.DayOfWeek - endD.DayOfWeek) * 2) / 7;

   if (endD.DayOfWeek == DayOfWeek.Saturday) calcBusinessDays--;
   if (startD.DayOfWeek == DayOfWeek.Sunday) calcBusinessDays--;
   return calcBusinessDays;
}