# Difference between dates, in years

**URL:** <https://discourse.julialang.org/t/difference-between-dates-in-years/130997>\
**Category:** General Usage\
**Tags:** dates, arithmetic\
**Created:** [July 24, 2025, 6:42am UTC](https://discourse.julialang.org/t/difference-between-dates-in-years/130997 "2025-07-24T06:42:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [July 24, 2025, 6:42am UTC](https://discourse.julialang.org/t/difference-between-dates-in-years/130997/1 "2025-07-24T06:42:30Z")

</div>

The difference between `Dates.Date` returns a `Day` object. I want to get that number of days “in years”. I’ve been doing stuff like this `d/Day(1)/365.25`, but repeatedly typing number literals is so ripe for a typo, let alone all the standard [time hazards](https://infiniteundo.com/post/25326999628/falsehoods-programmers-believe-about-time). Is there a better way?

```julia
julia> let d = Date(2010, 01, 01) - Date(2000, 01, 01)
           d/Day(1)/365.25
       end
10.001368925393566

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 24, 2025, 7:58am UTC](https://discourse.julialang.org/t/difference-between-dates-in-years/130997/2 "2025-07-24T07:58:54Z")

</div>

You may want to take a look at the DateFormats.jl package. However, the decimal result is slightly different:

```julia
using Dates, DateFormats
d1 = Date(2010, 01, 01)
d2 = Date(2000, 01, 01)
yeardecimal(d1 - d2) # 10.001574296529018

```

The package may use an average Gregorian year, said to be **365.2425 days**

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [July 24, 2025, 10:33am UTC](https://discourse.julialang.org/t/difference-between-dates-in-years/130997/3 "2025-07-24T10:33:46Z")

</div>

What does a difference between dates “in years” mean to you, what is a useful definition for your purposes? Your current method of dividing by 365.25 only makes sense if the interval between the dates has exactly 3:1 ratio of nonleap vs leap years. Otherwise it’s off and is either a little too high or a little too low.

For eg. `Date(2008, 01, 01) -ʸ Date(2005, 01, 01)` (where I use `-ʸ` to mean difference in years) has no leap year in the interval, but any calculation that takes the `Day` difference first (both your version and rafael’s `yeardecimal` version) loses that information about leap years and so cannot take that into account.

Another question to help get the precise definition of “difference in years” that makes sense to you: would you expect `Date(2008, 12, 31) -ʸ Date(2007, 12, 31)` to return 1 year? How about `Date(2007, 12, 31) -ʸ Date(2006, 12, 31)`? (2008 is a leap year and 2007 is not.)

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [July 24, 2025, 4:00pm UTC](https://discourse.julialang.org/t/difference-between-dates-in-years/130997/4 "2025-07-24T16:00:28Z")

</div>

> [@rafael.guerra](#):
>
> `yeardecimal(d1 - d2)`

`yeardecimal` of date difference fundamentally has to assume some fixed year duration. Most often that’s fine, but there’s also a more well-defined operation:

```julia
yeardecimal(d1) - yeardecimal(d2)

```

Here, each date is converted to the decimal part of that specific year, making the meaning clear and unambiguous.
