There’s a bug with the time_since function. It seems years are hardcoded to be exactly 365 days, including leap years.
Example:
Current date and time is 21 May 2025, 22:07:16h
{{ time_since(‘2025-05-21’ | as_datetime,0) }} = 22 hours 7 minutes 16 seconds - no leap year
{{ time_since(‘2024-05-21’ | as_datetime,0) }} = 1 year 22 hours 7 minutes 16 seconds - no leap year
{{ time_since(‘2023-05-21’ | as_datetime,0) }} = 2 years 1 day 22 hours 7 minutes 16 seconds - 1 additional day from leap year 2024 not considered
{{ time_since(‘2019-05-21’ | as_datetime,0) }} = 2 years 2 days 22 hours 7 minutes 16 seconds - 2 additional days from leap years 2020 and 2024 not considered
Wow all my favourite guys are here, thanks for all the comments. Anyway, I think the issue is pretty evident. If today is 2025-05-21, I’d consider 2023-05-21 as exactly 2 years ago, not 2 years and 1 day ago. And 50 years ago today would be 1975-05-21, not 1975-06-03.
For now, I’m using {{ (now() - (‘2023-05-21’ | as_datetime | as_local)) }} to report the time period between two dates. It only reports the total number of days + hms… not ideal but at least it’s accurate.
It’s calculating elapsed time since the start of the day on 2023-05-21 so that means 00:00:00. Elapsed time since 2023-05-21 and today is 1 year and several hours (depending on your time zone). The precision option performs rounding.
time_since(datetime, precision) converts a datetime object into its human-readable time string. The time string can be in seconds, minutes, hours, days, months, and years. precision takes an integer (full number) and indicates the number of units returned. The last unit is rounded. For example: precision = 1 could return “2 years” while precision = 2 could return “1 year 11 months”. This function can also be used as a filter. If the datetime is in the future, returns 0 seconds. A precision of 0 returns all available units, default is 1.
The difference is, all the algorithm sees is the difference in time, it does not know the start or end, so it can’t be fixed. This is a very common problem with durations in all software.
It’s currently 12:31 so that accounts for the 12 hours and 31 minutes.
With precision=0 I get:
2 years 1 day 12 hours 33 minutes 14 seconds
One leap year since 2023:
2024
Based on these results, it appears to report a year exclusively as a multiple of 365 days. Leap days are computed as additional days. So 2023-05-21 is 2 years and 1 day from 2025-05-21.
FWIW, that does seem like an unusual way of reporting elapsed time.
Aye, I’ve gone ahead and created my own code (with AI’s help) to handle the requirement. It’s crude and not performing any error checks but it works. Any pointers to shorten/optimise it would be greatly appreciated.
{% set target_date_str = "2023-05-21" %}
{% set target_date = strptime(target_date_str, '%Y-%m-%d').date() %}
{% set current_date = now().date() %}
{% set years = current_date.year - target_date.year %}
{% set months = current_date.month - target_date.month %}
{% set days = current_date.day - target_date.day %}
{% if days < 0 %}
{% set months = months - 1 %}
{% set days_in_prev_month = (current_date.replace(day=1) - timedelta(days=1)).day %}
{% set days = days + days_in_prev_month %}
{% endif %}
{% if months < 0 %}
{% set years = years - 1 %}
{% set months = months + 12 %}
{% endif -%}
{% if years > 0 %}{{ years }} years{% endif %}{% if months > 0 %} {{ months }} months{% endif %}{% if days > 0 %} {{ days }} days{% endif %}
[ps] This being my first thread creation, am I somehow supposed to mark it as closed when a conclusion has been reached?