# Convert Date difference to Float64

**URL:** <https://discourse.julialang.org/t/convert-date-difference-to-float64/79016>\
**Category:** New to Julia\
**Tags:** dates\
**Created:** [April 4, 2022, 6:25pm UTC](https://discourse.julialang.org/t/convert-date-difference-to-float64/79016 "2022-04-04T18:25:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [April 4, 2022, 6:25pm UTC](https://discourse.julialang.org/t/convert-date-difference-to-float64/79016/1 "2022-04-04T18:25:52Z")

</div>

[**Edit:** Sorry, folks, I’ve realized that **I simplified my actual problem too much.** The solutions you kindly offered are solutions to my example, but they don’t work for my actual problem! To avoid confusion, I post a more realistic problem above my original posting.]

I first calculate the center of a month and then need to calculate how many days there are between a time origin and the center of the month.

```julia
using Dates
const t0 = DateTime(1950,1,1) # origin
const t1 = DateTime(1986,1,1)
const t2 = t1 + Dates.Month(1)
const tm = t1 + (t2 - t1)/2 # center of the month
display(tm)
# LoadError: InexactError: divexact(Int64, 13164.5)
tm_in_days = Day(tm - t0)

```

The problem is the conversion of the interval `tm - t0` to `Day` fails.

Of course, we can utilize the ideas posted below to convert both `t1 - t0` and `t2 - t1` to integer days and then calculate t1 - t0 + (t2 - t1)/2 all in days.

But that’d be so inelegant a solution that I suspect there are better ways.

* * *

**[Original posting]**  
I’m wondering how to convert the difference between two `DateTime`s in days to a Float64:

```julia
using Dates
const t1 = DateTime(1986,1,1)
const t2 = DateTime(1986,2,1)

#s = Second(t2 - t1) / 2
d = Day(t2 - t1) / 2 # I want 15.5::Float64

```

The error is `ERROR: LoadError: InexactError: Int64(15.5)`. How does one get 15.5 as Float64 ?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [April 4, 2022, 6:30pm UTC](https://discourse.julialang.org/t/convert-date-difference-to-float64/79016/2 "2022-04-04T18:30:27Z")

</div>

```julia
julia> d = Day(t2 - t1)
31 days

julia> d.value/2
15.5

```

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [April 4, 2022, 6:50pm UTC](https://discourse.julialang.org/t/convert-date-difference-to-float64/79016/3 "2022-04-04T18:50:33Z")

</div>

```julia
julia> Day(t2 - t1) / Day(2)
15.5

```

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [April 5, 2022, 4:00pm UTC](https://discourse.julialang.org/t/convert-date-difference-to-float64/79016/4 "2022-04-05T16:00:27Z")

</div>

Julia avoids auto-rounding for arithmetic operations, so you need to explicitly round the result:

```julia
julia> round(tm - t0, Day)
13165 days

```

As an aside, it’s recommended to post a reply if you edit your original post, otherwise no notifications are sent and your edit might go unseen.

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [April 6, 2022, 8:16am UTC](https://discourse.julialang.org/t/convert-date-difference-to-float64/79016/5 "2022-04-06T08:16:52Z")

</div>

Thank you for your input, but unfortunately, that’s not what I need. I need a Float64 representation of `tm - t0` in days. That is, the value should be `13164.5`.

> it’s recommended to post a reply if you edit your original post

Thank you. That’s definitely important advice.

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [April 6, 2022, 8:24am UTC](https://discourse.julialang.org/t/convert-date-difference-to-float64/79016/6 "2022-04-06T08:24:27Z")

</div>

By the way, how do you find details of a library module like “Dates”. I spent quite a lot of time on the official documentation [Dates · The Julia Language](https://docs.julialang.org/en/v1/stdlib/Dates/) , but I haven’t found a description of the `value` attribute of the `Period` type or of the division of `Period` by another `Period`, which is used in the excellent answers to my original posting.

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [April 6, 2022, 8:28am UTC](https://discourse.julialang.org/t/convert-date-difference-to-float64/79016/7 "2022-04-06T08:28:09Z")

</div>

Use the source, Luke.

[https://github.com/JuliaLang/julia/tree/master/stdlib/Dates](https://github.com/JuliaLang/julia/tree/master/stdlib/Dates)

[https://github.com/JuliaLang/julia/blob/master/stdlib/Dates/src/periods.jl](https://github.com/JuliaLang/julia/blob/master/stdlib/Dates/src/periods.jl)

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [April 6, 2022, 8:29am UTC](https://discourse.julialang.org/t/convert-date-difference-to-float64/79016/8 "2022-04-06T08:29:00Z")

</div>

You could do `Hour(tm-t0)/Hour(24)`.

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [April 6, 2022, 9:10am UTC](https://discourse.julialang.org/t/convert-date-difference-to-float64/79016/9 "2022-04-06T09:10:10Z")

</div>

Thank you all for your help. So, it seems that the Dates module isn’t designed to make it straightforward to obtain an approximate floating-point representation of a `Period`.

So, I guess the most general solution is

```julia
using Dates
const t0 = DateTime(1950,1,1) # origin
const t1 = DateTime(1986,1,1)
const t2 = t1 + Dates.Month(1)
const tm = t1 + (t2 - t1) ÷ 7 # 1/7 of the month
println(tm)
tm_in_days = Microsecond(tm-t0)/Microsecond(24*60*60*1e6)
tm_in_days2 = round(tm-t0, Second)/Second(24*60*60) # approximate
println(tm_in_days, ", ", tm_in_days2)

```

I’ve noticed that the floating point division `/` doesn’t always work on a `Period` because it’s ultimately a kind of integer representing the time interval down to microseconds.

So, to get a Float64 number which is as accurate as possible, we convert the Period to an integer microsecond and then divid it by a day in microsecond.
