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

后端 未结 19 980
野的像风
野的像风 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:53

    Slightly more optimized version:

        DateTime Now = DateTime.Now;
    
        DateTime TempDate = new DateTime(Now.Year, Now.Month, 1);
    
        // find first friday
        while (TempDate.DayOfWeek != DayOfWeek.Friday)
            TempDate = TempDate.AddDays(1);
    
        // add two weeks
        TempDate = TempDate.AddDays(14);
    

提交回复
热议问题