Template sensor: delta months between 2 dates

i want wo create a template sensor that gives me the number of months between two given dates:

start_date = 2022-01-11
end_date = current_date

months = ???

somebody can help out?

{% set d = '2022-01-11' | as_datetime | as_local %}
{% set t = now() %}
{{ (t.year - d.year) * 12 + t.month - d.month }}

Keep in mind, this will only work for long term deltas. There are edge cases that will not work. Like less than 1 month apart will return 1. e.g. may 1st minus april 30th will result in 1, when it technically should be 0.

if you want a different approximation…

{% set d = '2022-01-11' | as_datetime | as_local %}
{% set t = now() %}
{{ (t - d).days // 31 }}

The major issue is that months don’t have the same number of days, so the only quick methods are approximations. Otherwise it’s heavy logic trying to find the number of months.

thank you very much!