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

后端 未结 19 988
野的像风
野的像风 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条回答
  •  萌比男神i
    2020-11-27 06:34

    I pass this the DateTime for the start of the month I am looking at.

        private DateTime thirdSunday(DateTime timeFrom)
        {
            List days = new List();
            DateTime testDate = timeFrom;
    
            while (testDate < timeFrom.AddMonths(1))
            {
                if (testDate.DayOfWeek == DayOfWeek.Friday)
                {
                    days.Add(testDate);
                }
                testDate = testDate.AddDays(1);
            }
    
            return days[2];
        }
    

提交回复
热议问题