Saturday, May 25, 2013

Function to Count Saturday and Sunday between two dates using C#

  1. This function returns count of falling Saturday and Sunday in the given date range.

public static int CountWeekEnds(DateTime startDate, DateTime endDate)
        {
            int weekEndCount = 0;

            if (startDate > endDate)
            {
                DateTime temp = startDate;
                startDate = endDate;
                endDate = temp;
            }

            TimeSpan diff = endDate - startDate;
            int days = diff.Days;

            for (var i = 0; i <= days; i++)
            {
                var testDate = startDate.AddDays(i);

           if(testDate.DayOfWeek==DayOfWeek.Saturday || testDate.DayOfWeek==DayOfWeek.Sunday)
                {              
                        weekEndCount += 1;                       
                }
            }

            return weekEndCount;
        }

1 comment:

  1. Thank you this function helped me to sort out issues i had!

    Thanks again

    ReplyDelete

SharePoint Deployment Error : The specified path, file name, or both are too long.

Hi, Sometimes we get surprised with below error while deploying wsp file... Solution : To overcome with this issue, we just ne...