# How to start the execution of a loop at a given time of day

**URL:** <https://discourse.julialang.org/t/how-to-start-the-execution-of-a-loop-at-a-given-time-of-day/85561>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [August 9, 2022, 10:52pm UTC](https://discourse.julialang.org/t/how-to-start-the-execution-of-a-loop-at-a-given-time-of-day/85561 "2022-08-09T22:52:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [August 9, 2022, 10:52pm UTC](https://discourse.julialang.org/t/how-to-start-the-execution-of-a-loop-at-a-given-time-of-day/85561/1 "2022-08-09T22:52:49Z")

</div>

I am trying to get Julia to execute a loop everyday at a given time, e.g. 18:00:00. I would just use the sleep function but the amount of time required to complete the function varies by day so this would cause the start time to slip on a daily basis. Instead I am trying something like the following.

```julia
while true 
    run function 
    w = Dates.Time(18,00,00)-Dates.Time(Dates.now())
    wait = w.value/(1*10^9)     
    sleep(wait)
end

```

Just wondering if this was the right approach or if there was a more efficient way to go about it. Thanks!

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [August 10, 2022, 4:17am UTC](https://discourse.julialang.org/t/how-to-start-the-execution-of-a-loop-at-a-given-time-of-day/85561/2 "2022-08-10T04:17:28Z")

</div>

I don’t know if there’s a better approach, but I wanted to remind you to `mod` your waiting time by 24h in case the function finishes before midnight.

---

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [August 19, 2022, 5:38pm UTC](https://discourse.julialang.org/t/how-to-start-the-execution-of-a-loop-at-a-given-time-of-day/85561/3 "2022-08-19T17:38:55Z")

</div>

thank you so much for pointing that out!
