Created
November 5, 2012 08:23
-
-
Save mikeminutillo/4015997 to your computer and use it in GitHub Desktop.
How to determine the Wednesday's in a given date range
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment