.Net DizzyCoding

How to get a list of week days in a month?

Problem :

In this other question it shows how to get all days of a month. I need the same thing, but I only want to list days of week (I want to exclude weekends).

How can I get a list of days of a month excluding weekends?

Solution :

Well, how about:

public static List<DateTime> GetDates(int year, int month)
{
   return Enumerable.Range(1, DateTime.DaysInMonth(year, month))
                    .Select(day => new DateTime(year, month, day))
                    .Where(dt => dt.DayOfWeek != DayOfWeek.Sunday &&
                                 dt.DayOfWeek != DayOfWeek.Saturday)
                    .ToList();
}
Exit mobile version