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

后端 未结 19 905
野的像风
野的像风 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 07:00

    Here's my algorithm:

    1. Find the number of days until the upcoming Friday.
    2. Initialize a counter and set it to 1. Subtract seven days from the date returned from [1], then compare the month from the date returned against the date returned from (1).
      1. If the months are not equal, return the counter from [2].
      2. If the months are equal, recurse into [2] and add 1 to the counter created in [2].

    The counter will give you the nth Friday of the month for that date (or its upcoming Friday).

    0 讨论(0)
提交回复
热议问题