# Datetime difference

**URL:** <https://discourse.julialang.org/t/datetime-difference/90075>\
**Category:** General Usage\
**Tags:** question, dates\
**Created:** [November 10, 2022, 9:56pm UTC](https://discourse.julialang.org/t/datetime-difference/90075 "2022-11-10T21:56:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [November 10, 2022, 9:56pm UTC](https://discourse.julialang.org/t/datetime-difference/90075/1 "2022-11-10T21:56:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [November 11, 2022, 1:56am UTC](https://discourse.julialang.org/t/datetime-difference/90075/2 "2022-11-11T01:56:42Z")

</div>

```julia
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
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](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](https://github.com/oxinabox/Pipe.jl)

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [November 11, 2022, 5:37am UTC](https://discourse.julialang.org/t/datetime-difference/90075/3 "2022-11-11T05:37:59Z")

</div>

Another way using the above `DateTime` variables is

```julia
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](https://docs.julialang.org/en/v1.7/stdlib/Dates/#Conversion-Functions).

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [November 11, 2022, 2:36pm UTC](https://discourse.julialang.org/t/datetime-difference/90075/4 "2022-11-11T14:36:47Z")

</div>

> [@jd-foster](#):
>
> `d = Dates.format(Dates.epochms2datetime(p.value),"H:M:S")`

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

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [November 11, 2022, 6:19pm UTC](https://discourse.julialang.org/t/datetime-difference/90075/5 "2022-11-11T18:19:13Z")

</div>

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
