# Find date-interval for ±n weekdays

**URL:** <https://discourse.julialang.org/t/find-date-interval-for-n-weekdays/12780>\
**Category:** General Usage\
**Tags:** question, proposal, dates\
**Created:** [August 1, 2018, 9:49am UTC](https://discourse.julialang.org/t/find-date-interval-for-n-weekdays/12780 "2018-08-01T09:49:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [August 1, 2018, 9:49am UTC](https://discourse.julialang.org/t/find-date-interval-for-n-weekdays/12780/1 "2018-08-01T09:49:10Z")

</div>

I needed to find the dates for a plus-minus `n` weekdays interval, you know, when they say the product will be delivered in 5 working/week days. I couldn’t find anything, so I made this:

```julia
function weekdaysinterval(today, weekdays)
    function step(x, by)
        counter = 0
        while counter < weekdays
            x += by
            if Dates.dayofweek(x) < 6
                counter += 1
            end
        end
        x
    end
    from = step(today, -Dates.Day(1))
    to = step(today, Dates.Day(1))
    from, to
end

```

Happy to hear if this is already implemented or if you have any suggestions. Note that this implementation is for _week_days, **not** _working_days, and as such it assumes that a week is Monday–Friday, including (however, this is not true for some countries, e.g. Israel).

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 1, 2018, 10:17am UTC](https://discourse.julialang.org/t/find-date-interval-for-n-weekdays/12780/2 "2018-08-01T10:17:33Z")

</div>

I don’t know how long the intervals are, but the number of days to add should be possible to obtain in a closed form.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [August 1, 2018, 10:54am UTC](https://discourse.julialang.org/t/find-date-interval-for-n-weekdays/12780/3 "2018-08-01T10:54:36Z")

</div>

> [@Tamas\_Papp](#):
>
> the number of days to add should be possible to obtain in a closed form

Of course 🤦‍♂️

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [August 1, 2018, 11:07am UTC](https://discourse.julialang.org/t/find-date-interval-for-n-weekdays/12780/4 "2018-08-01T11:07:42Z")

</div>

I always assumed

> **[GitHub - invenia/Holidays.jl: Julia library for handling holidays](https://github.com/invenia/Holidays.jl)**
>
> Julia library for handling holidays. Contribute to invenia/Holidays.jl development by creating an account on GitHub.

did this,  
but it doesn’t

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [August 1, 2018, 11:14am UTC](https://discourse.julialang.org/t/find-date-interval-for-n-weekdays/12780/5 "2018-08-01T11:14:37Z")

</div>

> [@Tamas\_Papp](#):
>
> closed form

How about this:

```julia
weekdays(today, weekdays) = today + Dates.Day(weekdays + 2*((Dates.dayofweek(today) + weekdays) ÷ 5))

```

Seems to give the exact same results as my former function…

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [August 1, 2018, 11:17am UTC](https://discourse.julialang.org/t/find-date-interval-for-n-weekdays/12780/6 "2018-08-01T11:17:08Z")

</div>

But with this info I could do workingdays-intervals. I guess I’ll have to use the open-form function from before.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 1, 2018, 11:35am UTC](https://discourse.julialang.org/t/find-date-interval-for-n-weekdays/12780/7 "2018-08-01T11:35:29Z")

</div>

Looks OK to me, but I am sure I would be tripped up by corner cases. Does it work for negative weekdays?

Also, while technically not a problem, I would use different names for the function and the argument.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [August 1, 2018, 11:36am UTC](https://discourse.julialang.org/t/find-date-interval-for-n-weekdays/12780/8 "2018-08-01T11:36:54Z")

</div>

Yea, for sure, I should check it out and make sure it always works. To be honest, I was sure someone will point me to an already existing implementation. Seems like a really common need…?

---

<div class="post-metadata">

**Author:** ![laborg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laborg/32/5474_2.png) [@laborg](https://discourse.julialang.org/u/laborg)\
**Post date:** [August 1, 2018, 12:08pm UTC](https://discourse.julialang.org/t/find-date-interval-for-n-weekdays/12780/9 "2018-08-01T12:08:37Z")

</div>

I think there is a one off error (at least it skips Friday) and I don’t think it works for negative values.

Here is some code that should suffice:

```julia
function weekdays(today, wd)
    function skipdays(workdays, today)
        adj = (Dates.dayofweek(today) - 1) * sign(workdays)
        weekends = div(workdays + adj, 5) 
        today + Dates.Day(workdays + weekends * 2)
    end

    skipdays(-wd, today), skipdays(wd, today)
end

```

There is also the Dates adjuster API with `Dates.toprev` and `Dates.tonext` that can be used to find such dates iteratively, but in a generic manner (e.g. add holidays into the mix).
