# Issue when Converting object of Type day to year

**URL:** <https://discourse.julialang.org/t/issue-when-converting-object-of-type-day-to-year/20560>\
**Category:** General Usage\
**Tags:** dates\
**Created:** [February 7, 2019, 7:40pm UTC](https://discourse.julialang.org/t/issue-when-converting-object-of-type-day-to-year/20560 "2019-02-07T19:40:40Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erisme](https://avatars.discourse-cdn.com/v4/letter/e/96bed5/32.png) [@Erisme](https://discourse.julialang.org/u/Erisme)\
**Post date:** [February 7, 2019, 7:40pm UTC](https://discourse.julialang.org/t/issue-when-converting-object-of-type-day-to-year/20560/1 "2019-02-07T19:40:40Z")

</div>

Hi,

I think I am having a bug when I want to convert number of days between 2 dates into years:  
I have:  
DateFinObservation = Dates.Date(2008,12,31)  
DateNaiss = Dates.Date.(t.DateNaissance,“mm/dd/yyyy”)

test= DateFinObservation - DateNaissance #will give me number of days between 2 dates in type days  
then if I want to make  
test2=Dates.year(test) # I have error message MethodError : no method matching +(::Day,::Int64)

Can you please help?

 ![Julia%20issue](https://global.discourse-cdn.com/julialang/original/3X/6/a/6a235d4b1ec4de9e1eb164c33f02f1bafcbfedd0.png)

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [February 7, 2019, 8:02pm UTC](https://discourse.julialang.org/t/issue-when-converting-object-of-type-day-to-year/20560/2 "2019-02-07T20:02:36Z")

</div>

What you were looking for is `Dates.Year`, but that does not work the way you want it either because a number of days cannot be converted into years as years have a variable number of days.

Two possible solutions are:

```julia
julia> Dates.year(d1) - Dates.year(d2)
9

julia> Dates.days(d1-d2)/365.25
9.100616016427105

```

PS: have a read through [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757).

---

<div class="post-metadata">

**Author:** ![Erisme](https://avatars.discourse-cdn.com/v4/letter/e/96bed5/32.png) [@Erisme](https://discourse.julialang.org/u/Erisme)\
**Post date:** [February 7, 2019, 9:15pm UTC](https://discourse.julialang.org/t/issue-when-converting-object-of-type-day-to-year/20560/3 "2019-02-07T21:15:06Z")

</div>

I understand but:  
Let’s say that NumDays = d1-d2 = 367 days  
if I make Date.year (NumDays) I have an error message  
if I make Date.year (367) i Have the right answer

As a scientific calculation I should be able to calculate datediff(d1,d2,yyyy) like we have in vba or R  
Is that something that someone can work on the package Dates?

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [February 7, 2019, 9:50pm UTC](https://discourse.julialang.org/t/issue-when-converting-object-of-type-day-to-year/20560/4 "2019-02-07T21:50:45Z")

</div>

If you want the number of years as a fraction, there are many conventions in use: [DayCounts.jl](https://github.com/JuliaFinance/DayCounts.jl) attempts to cover them all.

However it sounds like you want the largest integer `n` such that `dt1 + Year(n) <= dt2`, in which case you could do something like:

```julia
function nyears(dt1, dt2)
    n = div(dt2 - dt1, Day(365))
    dt1 + Year(n) <= dt2 ? n : n-1
end

```

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [February 7, 2019, 10:19pm UTC](https://discourse.julialang.org/t/issue-when-converting-object-of-type-day-to-year/20560/5 "2019-02-07T22:19:59Z")

</div>

> [@Erisme](#):
>
> Let’s say that NumDays = d1-d2 = 367 days  
> if I make Date.year (NumDays) I have an error message  
> if I make Date.year (367) i Have the right answer

```julia
julia> d1 = Dates.Date(2010,1,2)
2010-01-02

julia> d2 = Dates.Date(2008,12,31)
2008-12-31

julia> numdays = d1 - d2
367 days

julia> Dates.year(numdays)
ERROR: MethodError: no method matching +(::Day, ::Int64)

julia> Dates.year(Dates.value(numdays))
2

```

Note: This is not the same as `DateDiff` in VB (which only looks at the two years, not the months and days).

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [February 7, 2019, 10:39pm UTC](https://discourse.julialang.org/t/issue-when-converting-object-of-type-day-to-year/20560/6 "2019-02-07T22:39:50Z")

</div>

Note that `Dates.year` with an integer argument is undocumented: it effectively gives you the number of [Rata Die](https://en.wikipedia.org/wiki/Rata_Die) years: see [julia/accessors.jl at master · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/blob/master/stdlib/Dates/src/accessors.jl)

---

<div class="post-metadata">

**Author:** ![Erisme](https://avatars.discourse-cdn.com/v4/letter/e/96bed5/32.png) [@Erisme](https://discourse.julialang.org/u/Erisme)\
**Post date:** [February 7, 2019, 10:57pm UTC](https://discourse.julialang.org/t/issue-when-converting-object-of-type-day-to-year/20560/7 "2019-02-07T22:57:14Z")

</div>

Thank you very much everyone. Actually Dates.values is very useful as well I tried a trick that works I did Dates.year(Dates.day(startDate-EnDate))  
Otherwise I coded a function that replicate Datediff from vba

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [February 7, 2019, 11:11pm UTC](https://discourse.julialang.org/t/issue-when-converting-object-of-type-day-to-year/20560/8 "2019-02-07T23:11:13Z")

</div>

> [@Erisme](#):
>
> I coded a function that replicate Datediff from vba

Isn’t this just `year(d1) - year(d2)` ?

---

<div class="post-metadata">

**Author:** ![Erisme](https://avatars.discourse-cdn.com/v4/letter/e/96bed5/32.png) [@Erisme](https://discourse.julialang.org/u/Erisme)\
**Post date:** [February 8, 2019, 3:40am UTC](https://discourse.julialang.org/t/issue-when-converting-object-of-type-day-to-year/20560/9 "2019-02-08T03:40:32Z")

</div>

It is not the exact calculation of age as it should consider months and days

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [February 8, 2019, 7:13am UTC](https://discourse.julialang.org/t/issue-when-converting-object-of-type-day-to-year/20560/10 "2019-02-08T07:13:48Z")

</div>

Note that you cannot calculate the age of a person in years when you know their age in days. So your function will need to work on both dates and not on their difference:

```julia
julia> function age(dob, date)
       age = Dates.year(date)- Dates.year(dob) -1
       if Dates.month(date)>Dates.month(dob) || (Dates.month(date)==Dates.month(dob) && Dates.day(date)>=Dates.day(dob))
         age += 1
       end
       return age
       end

```

PS: have another read on how to quote your code: [PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530)

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [February 8, 2019, 9:54am UTC](https://discourse.julialang.org/t/issue-when-converting-object-of-type-day-to-year/20560/11 "2019-02-08T09:54:22Z")

</div>

Some alternatives to the above `age` function…

Emphasis on performance, using `yearmonthday`:

```julia
function age2(dob, date)
    y,m,d = yearmonthday(date) .- yearmonthday(dob)
    y - (m > 0 || (m == 0 && d >= 0) ? 0 : 1)
end

```

Emphasis on brevity and simplicity (still faster than `age`):

```julia
function age3(dob, date)
    y = year(date) - year(dob)
    y - (dob > date - Year(y) ? 1 : 0)
end

```

> **For reference, here's the above \`age\` function**
>
> ```julia
> function age(dob, date)
> age = Dates.year(date)- Dates.year(dob) -1
> if Dates.month(date)>Dates.month(dob) || (Dates.month(date)==Dates.month(dob) && Dates.day(date)>=Dates.day(dob))
> age += 1
> end
> return age
> end
> 
> ```

Are they all the same?

```julia
range = Date(1996,1,1):Day(1):Date(2010,1,1)
for d1 = range, d2 = range
    age(d1, d2) ≡ age2(d1, d2) ≡ age3(d1, d2) || println(d1," ",d2)
end

```

Yes (no output generated). How do they perform?

```julia
julia> @btime [age(d1, d2) for d1=$range, d2=$range];
  1.675 s (2 allocations: 199.61 MiB)

julia> @btime [age2(d1, d2) for d1=$range, d2=$range];
  874.106 ms (2 allocations: 199.61 MiB)

julia> @btime [age3(d1, d2) for d1=$range, d2=$range];
  1.470 s (2 allocations: 199.61 MiB)

```
