如何找到第三个星期五在一个月内用C#?第三个、一个月内、星期

2023-09-02 10:26:56 作者:婲逝﹌

给定一个日期,如何找到(类的DateTime )的第三个周五在该日期的月份?

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

推荐答案

我没有测试过这一点,但由于第三个星期五不可能在本月15日之前发生的,创建一个新的日期时间,然后就开始递增,直到你到了周五。

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);
}