public
Created

How to determine the Wednesday's in a given date range

  • Download Gist
gistfile1.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
void Main()
{
var start = new DateTime(2012, 10, 1);
var end = new DateTime(2012, 10, 31);
 
var days = from d in start.UpTo(end)
where d.DayOfWeek == DayOfWeek.Wednesday
select d;
 
days.Dump();
}
 
public static class Ext
{
public static IEnumerable<DateTime> UpTo(this DateTime start, DateTime end)
{
return start.Generate(x => x.AddDays(1)).TakeWhile(x => x <= end);
}
 
public static IEnumerable<T> Generate<T>(this T start, Func<T, T> getNext)
{
var current = start;
while(true)
{
yield return current;
current = getNext(current);
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.