Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia/Dates : convert a timespan from "days" to "year" or "Float64"

I would like to convert a timespan from "days" to "year" or "Float64"

let be at timespan t:

t = Date("2000-02-04") - Date("1996-06-04")

However each following lines give me an error

t/365
Float64(t)
parse(Float64,t)
convert(Dates.Year, t)
like image 823
ecjb Avatar asked Dec 01 '25 05:12

ecjb


1 Answers

You can get the value stored in t and divide it by 365

julia> Dates.value(t)/365
3.671232876712329

Note however that this assumes that each year is 365 days which is not true. For some scenarios a more elegant solution would be to count the years assuming that the date starts at some point, have a look at the example below:

julia> d0 = Date("2000-01-01")
2000-01-01

julia> d1 = d0 + t
2003-09-02

julia> year(d1)-year(d0), month(d1)-month(d0), day(d1) - day(d0)
(3, 8, 1)
like image 175
Przemyslaw Szufel Avatar answered Dec 04 '25 01:12

Przemyslaw Szufel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!