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