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!
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With