Datetime difference

Hello everyone.
I know that for calculations, the difference between two datetimes is useful, when in form of seconds (or another unit). My question is: to get this difference in form of days H:M:S, do I need to write a function or is there some easy trick?

Thanks.

julia> using Dates

julia> ts1 = DateTime("2019-10-01T12:00:00.00")
2019-10-01T12:00:00

julia> ts2 = DateTime("2019-10-02T03:23:40.00")
2019-10-02T03:23:40

julia> canonicalize(ts2-ts1)
15 hours, 23 minutes, 40 seconds

Some more manipulation:

julia> using Unitful

julia> using Pipe

julia> @pipe ustrip.(p.periods) |> join(_,":")
"15:23:40"

Regarding the use of Pipe, you can see endless discussions in recent thread here: https://discourse.julialang.org/t/fixing-the-piping-chaining-issue/89654 or just read the manual here (more useful): https://github.com/oxinabox/Pipe.jl

1 Like

Another way using the above DateTime variables is

julia> p = ts2 - ts1
55420000 milliseconds

julia> d = Dates.format(Dates.epochms2datetime(p.value),"H:M:S")
"15:23:40"

but note that you may need to check you’re not dropping the days off,cf. Conversion Functions.

if writing p.value feels like intruding into p internals, then p/Millisecond(1) also works (perhaps slower).

All the answers were useful for me, thank you all. I wanted to make sure I didn’t have anything like the python3 result, ie: days, H:M:S