How to find the 3rd Friday in a month with C#?

后端 未结 19 1057
野的像风
野的像风 2020-11-27 06:07

Given a date (of type DateTime), how do I find the 3rd Friday in the month of that date?

19条回答
  •  天命终不由人
    2020-11-27 06:45

    I haven't tested this, but since the third Friday can't possibly occur before the 15th of the month, create a new DateTime, then just increment until you get to a Friday.

    DateTime thirdFriday= new DateTime(yourDate.Year, yourDate.Month, 15);
    
    while (thirdFriday.DayOfWeek != DayOfWeek.Friday)
    {
       thirdFriday = thirdFriday.AddDays(1);
    }
    

提交回复
热议问题