# Get difference between two dates in seconds

**URL:** <https://discourse.julialang.org/t/get-difference-between-two-dates-in-seconds/11641>\
**Category:** New to Julia\
**Created:** [June 13, 2018, 12:11pm UTC](https://discourse.julialang.org/t/get-difference-between-two-dates-in-seconds/11641 "2018-06-13T12:11:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Atille](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atille/32/4412_2.png) [@Atille](https://discourse.julialang.org/u/Atille)\
**Post date:** [June 13, 2018, 12:11pm UTC](https://discourse.julialang.org/t/get-difference-between-two-dates-in-seconds/11641/1 "2018-06-13T12:11:36Z")

</div>

Hi everyone,

I am currently encountering an issue regarding the dates management with Julia.  
The case is that I have two dates, formated in a SQL-friendly date state and I would like to know the difference between them, in seconds.

Date 1 : 2018-06-13T04:40:49  
Date 2 : 2018-06-13T04:47:12

I tried to convert these dates in supported dates, but I have now to manage with milliseconds :

```julia
time_difference = Dates.DateTime(date2) - Dates.DateTime(date1)
println(time_difference)
383000 milliseconds

```

I cannot convert this milliseconds result into a seconds or hours results since I cannot perform a \*() function on it :

```julia
time_difference = (Dates.DateTime(date2) - Dates.DateTime(date1) * 1000)
println(time_difference)
ERROR: LoadError: MethodError: no method matching *(::DateTime, ::Int64)

```

Would you have any method regarding this point ? In addition, how could we get the timestamp related to these two dates ? Since this is about seconds, we could then substract one to the other to get the seconds between them.

Thanks in advance for your help!

---

<div class="post-metadata">

**Author:** ![Atille](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atille/32/4412_2.png) [@Atille](https://discourse.julialang.org/u/Atille)\
**Post date:** [June 13, 2018, 12:21pm UTC](https://discourse.julialang.org/t/get-difference-between-two-dates-in-seconds/11641/2 "2018-06-13T12:21:19Z")

</div>

Finally found !

```julia
time_difference = Int(convert(Dates.Second, (Dates.DateTime(date2) - Dates.DateTime(date1))))

```

It appears to be huge, but working.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [June 13, 2018, 12:22pm UTC](https://discourse.julialang.org/t/get-difference-between-two-dates-in-seconds/11641/3 "2018-06-13T12:22:02Z")

</div>

I don’t know if you want to keep the unit and thus round it to nearest second or something like:

```julia
julia> (Dates.DateTime(date2) - Dates.DateTime(date1))
6136 milliseconds

julia> (Dates.DateTime(date2) - Dates.DateTime(date1)) / Millisecond(1) * (1 / 1000)
6.136

```

---

<div class="post-metadata">

**Author:** ![Daneel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daneel/32/175_2.png) [@Daneel](https://discourse.julialang.org/u/Daneel)\
**Post date:** [June 13, 2018, 12:31pm UTC](https://discourse.julialang.org/t/get-difference-between-two-dates-in-seconds/11641/4 "2018-06-13T12:31:55Z")

</div>

Assuming you’re going for human-readable, I would do this.

```julia
julia> Dates.canonicalize(Dates.CompoundPeriod(Dates.DateTime(date2) - Dates.DateTime(date1)))
6 minutes, 23 seconds

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 13, 2018, 12:32pm UTC](https://discourse.julialang.org/t/get-difference-between-two-dates-in-seconds/11641/5 "2018-06-13T12:32:34Z")

</div>

> [@kristoffer.carlsson](#):
>
> `(Dates.DateTime(date2) - Dates.DateTime(date1)) / Millisecond(1) * (1 / 1000)`

Or just `/ Millisecond(1000)`

---

<div class="post-metadata">

**Author:** ![Atille](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atille/32/4412_2.png) [@Atille](https://discourse.julialang.org/u/Atille)\
**Post date:** [June 13, 2018, 4:21pm UTC](https://discourse.julialang.org/t/get-difference-between-two-dates-in-seconds/11641/6 "2018-06-13T16:21:49Z")

</div>

Thanks for the reply!

I just cannot notice how I can translate the dates (Y-m-d H:i:s) into a timestamp. Any method, or should I develop a function intending to do it ?

Regards,

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 13, 2018, 4:29pm UTC](https://discourse.julialang.org/t/get-difference-between-two-dates-in-seconds/11641/7 "2018-06-13T16:29:30Z")

</div>

> [@Atille](#):
>
> I just cannot notice how I can translate the dates (Y-m-d H:i:s) into a timestamp. Any method, or should I develop a function intending to do it ?

See [the conversion functions](https://docs.julialang.org/en/stable/stdlib/dates/#Conversion-Functions-1) to get various timestamps:

```julia
julia> using Dates

julia> datetime2unix(DateTime("2018-06-13T04:40:49"))
1.528864849e9

```

---

<div class="post-metadata">

**Author:** ![Atille](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atille/32/4412_2.png) [@Atille](https://discourse.julialang.org/u/Atille)\
**Post date:** [June 14, 2018, 3:17pm UTC](https://discourse.julialang.org/t/get-difference-between-two-dates-in-seconds/11641/8 "2018-06-14T15:17:51Z")

</div>

Hi,

Thanks for you reply, it appeared the Dates package wasn’t installed on my system. I can now convert my well formed hour to a good old timestamp.

Thanks again!
