13,772,614 members
Alternative Tip/Trick
alternative version

#### Stats

6.6K views
2 bookmarked
Posted 23 Feb 2016
Licenced CPOL

# Get a List of Dates for the Nth Desired Day of Week in a Month for a Range of Dates

, 24 Feb 2016
This is an alternative for "Get a list of Dates for the Nth Desired Day of Week in a Month for a range of dates"

## Introduction

The solution below is based on this tip by John Simmons and my alternative suggestions in its comments section.

## The Code

```public static List<DateTime> GetDatesByOrdinalDayV2(
DateTime startDate,
DateTime endDate,
DayOfWeek dayOfWeek,
int ordinal)
{
var foundDates = new List<DateTime>();
// Make sure ordinal parameter is within the allowable range
//If it's too high, set it to 5 so it returns the last
// qualifying day of each month
ordinal = Math.Min(Math.Max(ordinal, 1), 5);
while (startDate < endDate)
{
int month = startDate.Month;
//Set workingDate to the first day of the month.
var workingDate = new DateTime(startDate.Year, month, 1);
//Set workingDate to the first occurrence of the DayOfWeek parameter
workingDate = (int)workingDate.DayOfWeek > (int)dayOfWeek
? workingDate.AddDays(7 - (int)workingDate.DayOfWeek + (int)dayOfWeek)
: workingDate.AddDays((int)dayOfWeek - (int)workingDate.DayOfWeek);

//Advance the date by the number of days required to satisfy the ordinal parameter
workingDate = workingDate.AddDays((ordinal - 1) * 7);
//If the date has crossed the month boundary, step back a week.
//So the last occurrence of the target day is returned
workingDate = workingDate.Month != month ? workingDate.AddDays(-7) : workingDate;
}
return foundDates;
}
```

## Using the Code

Set the ordinal parameter to the occurrence in the month for the day of the week you require. So it's set to 1 for the first, 2 for the second, etc. If you enter 5 and there are not 5 occurrences in a particular month, you will get the date of the last occurrence in that month. Here's an example that returns the date of every second Friday of the month in 2016.

```private static void Main()
{
//Example, to get the date of every second Friday in the month for 2016
DateTime beg = DateTime.Parse("01/01/2016");
DateTime end = DateTime.Parse("01/01/2017");
List<DateTime> SecondFridays = GetDatesByOrdinalDayV2(beg, end, DayOfWeek.Friday, 2);
var dateStrings = SecondFridays
.Select(dateTime => dateTime.ToString("MMMM dd, yyyy")).ToList();
File.WriteAllLines(@"C:\Temp\DateTest.txt", dateStrings);
//writes
//January 08, 2016
//February 12, 2016
//March 11, 2016
//April 08, 2016
//May 13, 2016
//June 10, 2016
//July 08, 2016
//August 12, 2016
//September 09, 2016
//October 14, 2016
//November 11, 2016
//December 09, 2016
}
```

## About the Author

 Student Wales
No Biography provided

## Comments and Discussions

 First Prev Next
 Nice! Maciej Los1-Jun-16 2:37 Maciej Los 1-Jun-16 2:37
 Re: Nice! George Swan1-Jun-16 3:36 George Swan 1-Jun-16 3:36
 Nice improvement/elegantation! B. Clay Shannon24-Feb-16 5:56 B. Clay Shannon 24-Feb-16 5:56
 Re: Nice improvement/elegantation! George Swan24-Feb-16 6:56 George Swan 24-Feb-16 6:56
 Last Visit: 21-Nov-18 8:52     Last Update: 21-Nov-18 8:52 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.