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") - 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:

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

Something like this?

using Dates
ts1 = now()
sleep(61)
ts2 = now()
(ts2 - ts1) ÷ Millisecond(60000)
2 Likes
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
needs some effort to comprehend.

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

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

2 Likes