Let’s say we want to find the date of the first Friday of the 1st month.
GetFirstFridayOfMonth(01.01.2023) =>06.01.2023 (The first Friday after 01.01.2023 is 06.01.2023)
public DateTime GetFirstFridayOfMonth(DateTime date)
{
DateTime fridayDate = new DateTime(date.Year, date.Month, 1); //Let's assume 1st Friday can start on the 1st of the month
while (fridayDate.DayOfWeek != DayOfWeek.Friday)
{
fridayDate = fridayDate.AddDays(1);
}
return fridayDate;
}
Now let’s say we want to find the date of the third Tuesday of the 11th month.
GetFirstFridayOfMonth(15.11.2023) => 21.11.2023 (The first Tuesday after 15.11.2023 is 21.11.2023)
public DateTime GetThirdTuesdayOfMonth(DateTime date)
{
DateTime fridayDate = new DateTime(date.Year, date.Month, 15); //Let's assume 1st Friday can start on the 1st of the month
while (fridayDate.DayOfWeek != DayOfWeek.Tuesday)
{
fridayDate = fridayDate.AddDays(1);
}
return fridayDate;
}
Thanks to Pete for that great answer.
