# The duration between two times, expressed as nearest whole minutes

**URL:** <https://discourse.julialang.org/t/the-duration-between-two-times-expressed-as-nearest-whole-minutes/42429>\
**Category:** New to Julia\
**Created:** [July 2, 2020, 12:12pm UTC](https://discourse.julialang.org/t/the-duration-between-two-times-expressed-as-nearest-whole-minutes/42429 "2020-07-02T12:12:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [July 2, 2020, 12:12pm UTC](https://discourse.julialang.org/t/the-duration-between-two-times-expressed-as-nearest-whole-minutes/42429/1 "2020-07-02T12:12:08Z")

</div>

I have two times, t1 and t2 and I wish to find the duration between these. The standard answer provided by Julia appears to be in milliseconds (and Julia also states milliseconds as part of its output). However, I need the difference converted to an integer, which captures the difference in nearest whole minutes.

I seem to hit a snag when I divide by 60000 to convert, so I wonder what the proper method is.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 2, 2020, 12:17pm UTC](https://discourse.julialang.org/t/the-duration-between-two-times-expressed-as-nearest-whole-minutes/42429/2 "2020-07-02T12:17:30Z")

</div>

Please provide code that shows what you are currently doing.

---

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [July 2, 2020, 12:25pm UTC](https://discourse.julialang.org/t/the-duration-between-two-times-expressed-as-nearest-whole-minutes/42429/3 "2020-07-02T12:25:03Z")

</div>

The problem is part of a function that I have written, which first converts a strings to dates and times, and then subtracts these from now. The output is in milliseconds:

```julia
function timeToStart(stringTime)
    allTimes = []
    for i = 1:length(stringTime)
        time = Dates.DateTime(stringTime[i,1], "Y-mm-ddTH:MM:SS.SSSZ") - Dates.now()
        allTimes = [allTimes;time]
    end
    return allTimes
end

```

When I then take an element in the output, and divide by 60000, I get the error:

```julia
**InexactError: Int64(10.61605)**

in top-level scope at [ApiNgDemoJsonRpc.jl:318](#)

in / at [stdlib\v1.4\Dates\src\periods.jl:79](#)

in Millisecond at [stdlib\v1.4\Dates\src\types.jl:33](#)

in convert at [base\number.jl:7](#)

in Int64 at [base\float.jl:710](#)

```

---

<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:** [July 2, 2020, 12:25pm UTC](https://discourse.julialang.org/t/the-duration-between-two-times-expressed-as-nearest-whole-minutes/42429/4 "2020-07-02T12:25:19Z")

</div>

You are probably looking for something like this:

```julia
julia> using Dates

julia> a = now()
2020-07-02T14:23:24.311

julia> b = a + Second(130)
2020-07-02T14:25:34.311

julia> round(b - a, Minute) # implies RoundNearest
2 minutes

julia> round(b - a, Minute, RoundUp)
3 minutes

```

---

<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:** [July 2, 2020, 12:25pm UTC](https://discourse.julialang.org/t/the-duration-between-two-times-expressed-as-nearest-whole-minutes/42429/5 "2020-07-02T12:25:43Z")

</div>

Something like this?

```julia
using Dates
ts1 = now()
sleep(61)
ts2 = now()
(ts2 - ts1) ÷ Millisecond(60000)

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [July 2, 2020, 12:28pm UTC](https://discourse.julialang.org/t/the-duration-between-two-times-expressed-as-nearest-whole-minutes/42429/6 "2020-07-02T12:28:26Z")

</div>

```julia
julia> using Dates

julia> Dates.DateTime(Dates.now())+Dates.Day(5) - Dates.DateTime(Dates.now())
432000000 milliseconds

julia> Dates.Minute(Dates.DateTime(Dates.now())+Dates.Day(5) - Dates.DateTime(Dates.now()))
7200 minutes

julia> Dates.Day(Dates.DateTime(Dates.now())+Dates.Day(5) - Dates.DateTime(Dates.now()))
5 days

julia> Dates.Second(Dates.DateTime(Dates.now())+Dates.Day(5) - Dates.DateTime(Dates.now()))
432000 seconds

```

[https://docs.julialang.org/en/v1/stdlib/Dates/index.html](https://docs.julialang.org/en/v1/stdlib/Dates/index.html)  
needs some effort to comprehend.

And doesn’t give Int, I know. Just ignore.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [July 2, 2020, 12:38pm UTC](https://discourse.julialang.org/t/the-duration-between-two-times-expressed-as-nearest-whole-minutes/42429/7 "2020-07-02T12:38:22Z")

</div>

But  
`Dates.Minute(Dates.DateTime(Dates.now())+Dates.Day(5) - Dates.DateTime(Dates.now())).value`  
does return the Minutes in Int64.
