Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all weekends within date range in C# [closed]

I am just wondering if there is a simple way or framework to to get all weekends within date range in C#?

Is it possible to do with LINQ as well?

Any clue?

Thank you!

like image 528
Friend Avatar asked Jul 25 '14 18:07

Friend


1 Answers

If you make a way to enumerate all days, you can use linq to filter to weekends:

IEnumerable<DateTime> GetDaysBetween(DateTime start, DateTime end)
{
    for (DateTime i = start; i < end; i = i.AddDays(1))
    {
        yield return i;
    }
}

var weekends = GetDaysBetween(DateTime.Today, DateTime.Today.AddDays(365))
    .Where(d => d.DayOfWeek == DayOfWeek.Saturday || d.DayOfWeek == DayOfWeek.Sunday);
like image 198
Mitch Avatar answered Oct 14 '22 06:10

Mitch