Number of months between two dates

Is there a convenient way to calculate the number of months between two dates? I can’t seem to find anything in the Dates package.

I am not sure the question is well-defined, as months do not have a uniform length.

One convention I am aware of is using days, then dividing by 365.25/12 == 30.4375 (to account for leap years).

One simple way is

julia> using Dates

julia> start_date = Date(2019, 6, 27)
2019-06-27

julia> end_date = Date(2020, 6, 27)
2020-06-27

julia> length(start_date:Month(1):end_date)
13

(maybe requires some adjustment depending on how you want to deal with partial months, or whether the above should return 12 rather than 13 etc)

3 Likes