I’m creating a scheduler and need to be able to do the following in C#:
- Find the 1st Tuesday of June 2012
- Find the last Friday of March 2008
- Find every Saturday in January 2013
- Find the 3rd Friday in July 2009
- Find every Saturday over the next 3 months
- Find every day in March 2018
The results should come back as DateTime or List<DateTime>.
Here are methods to find the first/last specified day of week in a given month:
From those, you can easily find the answers to the other questions… just add 7 days to find the next occurence of the day you’re looking for
EDIT: thinking more about it, it would be pretty handy to have that in the form of extension methods, such as :
Here is the implementation :
It’s probably more flexible than the first methods I suggested… With that, you can easily do things like that :