Today I ‘fun need’ which I don’t ever recall coming across before and I thought I would share my results.
I needed to find both the number of Wednesdays (or any day for instance) and the date for each instance for a given date range. What I came up w/ was as follows (the code below is LINQPad C# but is 100% valid).
var startDate = new DateTime(2012, 10, 1);
var endDate = new DateTime(2012, 10, 31);
var totalDays = (int)endDate.Subtract(startDate).TotalDays + 1; // need to offset the math
var totalDates = Enumerable.Range(1, totalDays).Select(n => startDate.AddDays(n));
var days = totalDates.Where (x => x.DayOfWeek == DayOfWeek.Wednesday );
I thought this was fun and pretty simple. I am sure there are a thousand ways to do this, but i like this one.
Till next time,
10-19-2012 12:47 PM