# How to format or print a difference in two DateTime objects

**URL:** <https://discourse.julialang.org/t/how-to-format-or-print-a-difference-in-two-datetime-objects/121638>\
**Category:** New to Julia\
**Tags:** dates, datetime\
**Created:** [October 23, 2024, 1:03pm UTC](https://discourse.julialang.org/t/how-to-format-or-print-a-difference-in-two-datetime-objects/121638 "2024-10-23T13:03:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon91151494](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@anon91151494](https://discourse.julialang.org/u/anon91151494)\
**Post date:** [October 23, 2024, 1:03pm UTC](https://discourse.julialang.org/t/how-to-format-or-print-a-difference-in-two-datetime-objects/121638/1 "2024-10-23T13:03:04Z")

</div>

I want to print the following object, which is a difference between two `DateTime`s

```julia
datetime_diff = datetime2 - datetime1

```

The type of `datetime_diff` appears to be `Milliseconds`. That means it can be printed, but produces the following output which is not human readable

```julia
86398979 milliseconds

```

Sorry for asking such a basic question, but I cannot figure out how to format this in a more readable way.

Something like `HH:MM:SS` would be ideal.

---

<div class="post-metadata">

**Author:** ![anon91151494](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@anon91151494](https://discourse.julialang.org/u/anon91151494)\
**Post date:** [October 23, 2024, 1:36pm UTC](https://discourse.julialang.org/t/how-to-format-or-print-a-difference-in-two-datetime-objects/121638/2 "2024-10-23T13:36:58Z")

</div>

Managed to figure this out…

```julia
datetime1 = DateTime(2024, 10, 24, 9, 0, 0)
datetime2 = DateTime(2024, 10, 24, 9, 30, 0)
datetime_diff = datetime2 - datetime1
ms = Millisecond(datetime_diff) # btw: it is already of type Millisecond
time = Dates.Time(canonicalize(Dates.CompoundPeriod(ms)).periods...)
datetime_diff_str::String = Dates.format(time, "HH:MM:SS")
println("$(datetime_diff_str)")

```

However, it’s not great and I wish there was a more direct way to do such a thing. I would like to contribute something to the Dates library in Julia. I will put it on my list of things to do, and hopefully I will have some time to get around to it…

---

<div class="post-metadata">

**Author:** ![anon91151494](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@anon91151494](https://discourse.julialang.org/u/anon91151494)\
**Post date:** [October 23, 2024, 1:55pm UTC](https://discourse.julialang.org/t/how-to-format-or-print-a-difference-in-two-datetime-objects/121638/3 "2024-10-23T13:55:53Z")

</div>

I’ve also found some alternative approaches which may be useful to some.

```julia
julia> using Dates

julia> ms = Millisecond(12345678)
12345678 milliseconds

julia> Dates.value(ms)
12345678

julia> typeof(Dates.value(ms))
Int64

julia> i = Dates.value(ms)
12345678

# get number of seconds, minutes, hours etc
# by performing truncating division on the integer value
# formed from the number of milliseconds and then
# converting to the correct explicit type
# in other words:
# 1. get Milliseconds object
# 2. convert to integer type
# 3. truncating division
# 4. create Second/Minute/Hour type from integer value
# this is a "unitless" approach
julia> Second(div(i, 1000))
12345 seconds

julia> Minute(div(i, 1000 * 60))
205 minutes

```

```julia
# alternatively you can do it in a way which maintains the units
julia> Millisecond(12000) / Millisecond(1000)
12.0

julia> Millisecond(12000000) / Millisecond(1000 * 60)
200.0

julia> Millisecond(12000000) / Millisecond(1000 * 60 * 60)
3.3333333333333335

julia>

```

---

<div class="post-metadata">

**Author:** ![anon91151494](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@anon91151494](https://discourse.julialang.org/u/anon91151494)\
**Post date:** [October 23, 2024, 2:00pm UTC](https://discourse.julialang.org/t/how-to-format-or-print-a-difference-in-two-datetime-objects/121638/4 "2024-10-23T14:00:10Z")

</div>

Aha - and you can go further:

```julia
julia> Millisecond(12345678) / Second(1)
12345.678

julia> Millisecond(12345678) / Hour(1)
3.429355

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 23, 2024, 2:00pm UTC](https://discourse.julialang.org/t/how-to-format-or-print-a-difference-in-two-datetime-objects/121638/5 "2024-10-23T14:00:16Z")

</div>

> [@anon91151494](#):
>
> ```julia
> ms = Millisecond(datetime_diff) # btw: it is already of type Millisecond
> time = Dates.Time(canonicalize(Dates.CompoundPeriod(ms)).periods...)
> datetime_diff_str::String = Dates.format(time, "HH:MM:SS")
> println("$(datetime_diff_str)")
> 
> ```

All this code can be boiled down to:

```julia
t = Dates.Time(canonicalize(datetime_diff))
println("$t")

```

---

<div class="post-metadata">

**Author:** ![anon91151494](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@anon91151494](https://discourse.julialang.org/u/anon91151494)\
**Post date:** [October 23, 2024, 2:02pm UTC](https://discourse.julialang.org/t/how-to-format-or-print-a-difference-in-two-datetime-objects/121638/6 "2024-10-23T14:02:16Z")

</div>

This was one of my earlier approaches, but I found that it didn’t work.

```julia
julia> dt1 = DateTime(2024, 10, 22, 9, 30)
2024-10-22T09:30:00

julia> dt2 = DateTime(2024, 10, 22, 10, 1)
2024-10-22T10:01:00

julia> Dates.Time(canonicalize(dt2 - dt1))
ERROR: MethodError: no method matching Int64(::Dates.CompoundPeriod)
The type `Int64` exists, but no method is defined for this combination of argument types when trying to construct it.

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 23, 2024, 2:05pm UTC](https://discourse.julialang.org/t/how-to-format-or-print-a-difference-in-two-datetime-objects/121638/7 "2024-10-23T14:05:37Z")

</div>

It will work if you load, in addition to Dates.jl, the package CompoundPeriods.jl.

---

<div class="post-metadata">

**Author:** ![anon91151494](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@anon91151494](https://discourse.julialang.org/u/anon91151494)\
**Post date:** [October 23, 2024, 2:09pm UTC](https://discourse.julialang.org/t/how-to-format-or-print-a-difference-in-two-datetime-objects/121638/8 "2024-10-23T14:09:48Z")

</div>

Ok thank you, I will suggest my organization add that.
