Convert Date difference to Float64

[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.

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 DateTimes in days to a Float64:

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 ?

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

julia> d.value/2
15.5
1 Like
julia> Day(t2 - t1) / Day(2)
15.5
6 Likes

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

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.

2 Likes

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.

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 , 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.

Use the source, Luke.

https://github.com/JuliaLang/julia/tree/master/stdlib/Dates

https://github.com/JuliaLang/julia/blob/master/stdlib/Dates/src/periods.jl

2 Likes

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

2 Likes

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

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.