The duration between two times, expressed as nearest whole minutes

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.

Please provide code that shows what you are currently doing.

1 Like

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:

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

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

**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](#)

You are probably looking for something like this:

julia> using Dates

julia> a = now()

julia> b = a + Second(130)

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

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

Something like this?

using Dates
ts1 = now()
ts2 = now()
(ts2 - ts1) ÷ Millisecond(60000)
julia> using Dates

julia> Dates.DateTime( - Dates.DateTime(
432000000 milliseconds

julia> Dates.Minute(Dates.DateTime( - Dates.DateTime(
7200 minutes

julia> Dates.Day(Dates.DateTime( - Dates.DateTime(
5 days

julia> Dates.Second(Dates.DateTime( - Dates.DateTime(
432000 seconds
needs some effort to comprehend.

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

Dates.Minute(Dates.DateTime( - Dates.DateTime(
does return the Minutes in Int64.