Getting number of Sunday for a specific month

The  below code is for getting number of sunday for a current month

//First We find out last date of mont

DateTime today = DateTime.Today;
DateTime endOfMonth = new DateTime(today.Year, today.Month, DateTime.DaysInMonth(today.Year, today.Month));
//get only last day of month
int day = endOfMonth.Day;
        
DateTime now = DateTime.Now;
int count;
count = 0;
for (int i = 0; i < day; ++i)
{
            DateTime d = new DateTime(now.Year, now.Month, i + 1);
            //Compare date with sunday
            if (d.DayOfWeek == DayOfWeek.Sunday)
            {
                count = count + 1;
            }
}
Ebook Download
View all
Learn
View all