# Getting the third Friday of the month

**URL:** <https://discourse.julialang.org/t/getting-the-third-friday-of-the-month/56787>\
**Category:** General Usage\
**Tags:** question, dates\
**Created:** [March 9, 2021, 6:13am UTC](https://discourse.julialang.org/t/getting-the-third-friday-of-the-month/56787 "2021-03-09T06:13:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [March 9, 2021, 6:13am UTC](https://discourse.julialang.org/t/getting-the-third-friday-of-the-month/56787/1 "2021-03-09T06:13:41Z")

</div>

Given a particular month, I need to find the third Friday of that month. I just found out that there is a function called `dayofweekofmonth` which basically does the reverse of what I want. You give it a date and it tells you that date is in fact the third Friday of the month (it returns 3).

For example, today is the second Monday of the month

```julia
julia> dayofweekofmonth(Date("2021-03-08"))
2

```

I basically want to know if there is a shorthand for the inverse of this function (as in, something that returns the date itself) or if anybody has a cute trick to come up with it beyond running a boolean check on every date inside a given month.

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [March 9, 2021, 6:54am UTC](https://discourse.julialang.org/t/getting-the-third-friday-of-the-month/56787/2 "2021-03-09T06:54:47Z")

</div>

When I’ll have an access to my computer I’ll write full code, but idea is the following

1. Calculate day of the week for the first day of the month e.g. “2021-03-01”
2. Calculate the distance to the nearest Friday (number in range 0:6)
3. Add 14
4. Add result to the first day of the month

UPD:

```julia
function thirdfriday(dt)
    dt = floor(Date(dt), Month)
    dt |> dayofweek |> 
        (x -> 5 - x < 0 ? 12 - x : 5 - x) |> 
        (x -> x + 14) |> 
        (x -> dt + Day(x))
end

julia> dr = Date(2021):Month(1):Date(2022);
julia> thirdfriday.(dr)
13-element Vector{Date}:
 2021-01-15
 2021-02-19
 2021-03-19
 2021-04-16
 2021-05-21
 2021-06-18
 2021-07-16
 2021-08-20
 2021-09-17
 2021-10-15
 2021-11-19
 2021-12-17
 2022-01-21

```

It should be efficient, I suppose, since it adds no allocations, pure arithmetic.

It was interesting to read `Dates` documentation, there are interesting `tonext` and `toprev` functions as well as `filter`. So, if you are not afraid of iterations, at least they can be written in a very compact form

```julia
julia> tonext(x -> (dayofweek(x) == 5) && (dayofweekofmonth(x) == 3), Date("2020-03-01"))
2020-03-20

julia> dr = Date(2021):Day(1):Date(2022)
Date("2021-01-01"):Day(1):Date("2022-01-01")

julia> filter(dr) do x
           (dayofweek(x) == 5) && (dayofweekofmonth(x) == 3)
       end
12-element Vector{Date}:
 2021-01-15
 2021-02-19
 2021-03-19
 2021-04-16
 2021-05-21
 2021-06-18
 2021-07-16
 2021-08-20
 2021-09-17
 2021-10-15
 2021-11-19
 2021-12-17

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 9, 2021, 6:58am UTC](https://discourse.julialang.org/t/getting-the-third-friday-of-the-month/56787/3 "2021-03-09T06:58:31Z")

</div>

Not sure this is particularly clever but here’s how I would do it:

```julia
julia> using Dates

julia> third_friday(dates) = dates[findfirst(x -> (dayofweek(x) == 5) && (dayofweekofmonth(x) == 3), dates)]
third_friday (generic function with 1 method)

julia> candidates = Date(2021,1,15):Day(1):Date(2021,1,21)
Date("2021-01-15"):Day(1):Date("2021-01-21")

julia> [third_friday(candidates + Month(i)) for i ∈ 0:12]
13-element Vector{Date}:
 2021-01-15
 2021-02-19
 2021-03-19
 2021-04-16
 2021-05-21
 2021-06-18
 2021-07-16
 2021-08-20
 2021-09-17
 2021-10-15
 2021-11-19
 2021-12-17
 2022-01-21

```

This uses the fact that the third Friday must be between the 15th and 21st (which I suppose is a variation on the “find first Friday and add 14” theme above)

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [March 9, 2021, 8:32am UTC](https://discourse.julialang.org/t/getting-the-third-friday-of-the-month/56787/4 "2021-03-09T08:32:34Z")

</div>

Here is another possibility:

```julia
function get_next_expiry(calc_date:: Date, month:: Integer; holidays:: AbstractArray = [])
    # get 3rd Wednesday of the corresponding month
    expiry_date = Dates.tonext(calc_date + Month(month-1)) do x
       Dates.dayofweek(x) == Dates.Friday && Dates.dayofweekofmonth(x) == 3
    end
    # move to next business day if the day is a holiday - not sure if you need this
    Dates.tonext(expiry_date, same=true) do x
        isbusday(x, holidays=holidays)
    end
end

# get the next 300 3rd Fridays, shifted to next business day if required
expiry_dates = get_next_expiry.(START_DATE, 1:300; holidays=target_holidays)

```

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [March 9, 2021, 3:48pm UTC](https://discourse.julialang.org/t/getting-the-third-friday-of-the-month/56787/5 "2021-03-09T15:48:46Z")

</div>

Thanks for the responses. Extra points for @lungben for thinking ahead about business days (but I need them for the Eurex trading calendar, not sure that is built-in anywhere).

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [March 9, 2021, 4:14pm UTC](https://discourse.julialang.org/t/getting-the-third-friday-of-the-month/56787/6 "2021-03-09T16:14:15Z")

</div>

For most products, Eurex uses Target holiday calendar. Not sure if there is a free source, but it is available in Bloomberg or Reuters.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [March 9, 2021, 4:15pm UTC](https://discourse.julialang.org/t/getting-the-third-friday-of-the-month/56787/7 "2021-03-09T16:15:21Z")

</div>

Thanks! I’ll take a look.

---

<div class="post-metadata">

**Author:** ![jmkuhn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmkuhn/32/2090_2.png) [@jmkuhn](https://discourse.julialang.org/u/jmkuhn)\
**Post date:** [March 10, 2021, 12:05am UTC](https://discourse.julialang.org/t/getting-the-third-friday-of-the-month/56787/8 "2021-03-10T00:05:10Z")

</div>

Have a look at [BusinessDays.jl](https://github.com/JuliaFinance/BusinessDays.jl). This example uses an unexported function `findweekday`. Find the first business day ([TARGET2](https://www.ecb.europa.eu/paym/target/target2/profuse/calendar/html/index.en.html)) on or after the first Friday of January 2021.

```julia
julia> using BusinessDays, Dates

help?> BusinessDays.findweekday
  findweekday(weekday_target::Integer, yy::Integer, mm::Integer, occurrence::Integer, ascending::Bool) → Date

  Given a year yy and month mm, finds a date where a choosen weekday occurs.

  weekday_target values are declared in module Base.Dates:
  Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday = 1,2,3,4,5,6,7.

  If ascending is true, searches from the beginning of the month. If false,
  searches from the end of the month.

  If occurrence is 2 and weekday_target is Monday, searches the 2nd Monday of
  the given month, and so on.

julia> dow = Dates.Friday; y = 2021; m = 1; occurence = 1
1

julia> tobday(:TARGET2, BusinessDays.findweekday(dow, y, m, occurence, true))
2021-01-04

```

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [March 10, 2021, 12:28am UTC](https://discourse.julialang.org/t/getting-the-third-friday-of-the-month/56787/9 "2021-03-10T00:28:51Z")

</div>

Ah I was aware of BusinessDays.jl but did not know about this function.
