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?


julia> using Dates

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

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

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(_,":")

Regarding the use of Pipe, you can see endless discussions in recent thread here: or just read the manual here (more useful):

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

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