logo

How to Calculate the Difference Between Two Times in Seconds in Julia 📂Julia

How to Calculate the Difference Between Two Times in Seconds in Julia

Overview

To achieve this, one can use the canonicalize() function of the Dates module1.

Code

using Dates

tic = DateTime(2022,3,7,7,1,11)
toc = now()
Dates.canonicalize(toc-tic)

The result of executing the above code is as follows.

julia> using Dates

julia> tic = DateTime(2022,3,7,7,1,11)
2022-03-07T07:01:11

julia> toc = now()
2022-07-19T22:26:22.070

julia> Dates.canonicalize(toc-tic)
19 weeks, 1 day, 15 hours, 25 minutes, 11 seconds, 70 milliseconds

It automatically calculates and outputs up to weeks, precisely as multiples of smaller units.

Environment

  • OS: Windows
  • julia: v1.7.0