‘Friendly’ datetime labels

I’ve made a template to take datetime types and show them in a friendly or more-casual wording. For example:

  • Today
  • Tomorrow
  • This Wednesday
  • Next Wednesday
  • etc…

It ‘works’ for me however it’s built upon a lot of IF statements and seems like a code smell.

I’d love some feedback, if anyone knows of a cleaner way to achieve this, or if it’s useful for anyone else.

{% if as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%H%M') == "0000" %}
  {% if as_timestamp(state_attr('shortcuts.calendar', 'event_date')) |  timestamp_custom('%j') | int == 359 %}
    {{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%A')}}
  {% elif as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%D') == as_timestamp(now(), default) |  timestamp_custom('%D') %}
    Today
  {% elif as_timestamp(state_attr('shortcuts.calendar', 'event_date')) |  timestamp_custom('%d') | int == as_timestamp(now(), default) |  timestamp_custom('%d') | int +1 %}
    Tomorrow
  {% elif (as_timestamp(state_attr('shortcuts.calendar', 'event_date')) |  timestamp_custom('%j') | int - as_timestamp(now(), default) |  timestamp_custom('%j') | int ) < 6 %}
    {{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%A')}}
  {% elif (as_timestamp(state_attr('shortcuts.calendar', 'event_date')) |  timestamp_custom('%j') | int - as_timestamp(now(), default) |  timestamp_custom('%j') | int ) > 6 and as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%U') | int == as_timestamp(now(), default) |  timestamp_custom('%U') | int +1 %}
    Next {{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%A')}}
  {% else %}
    {{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%d/%m')}}
  {% endif %}
{% else %}
  {% if as_timestamp(state_attr('shortcuts.calendar', 'event_date')) |  timestamp_custom('%j') | int == 359 %}
    {{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%H:%M')}} {{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%A')}}
  {% elif as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%D') == as_timestamp(now(), default) |  timestamp_custom('%D') %}
    {{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%H:%M')}} Today
  {% elif as_timestamp(state_attr('shortcuts.calendar', 'event_date')) |  timestamp_custom('%d') | int == as_timestamp(now(), default) |  timestamp_custom('%d') | int +1 %}
    {{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%H:%M')}} Tomorrow
  {% elif (as_timestamp(state_attr('shortcuts.calendar', 'event_date')) |  timestamp_custom('%j') | int - as_timestamp(now(), default) |  timestamp_custom('%j') | int ) < 6 %}
    {{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%H:%M')}} {{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%A')}}
  {% elif (as_timestamp(state_attr('shortcuts.calendar', 'event_date')) |  timestamp_custom('%j') | int - as_timestamp(now(), default) |  timestamp_custom('%j') | int ) > 6 and as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%U') | int == as_timestamp(now(), default) |  timestamp_custom('%U') | int +1 %}
    {{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%H:%M')}} Next {{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%A')}}
  {% else %}
    {{as_timestamp(state_attr('shortcuts.calendar', 'event_date')) | timestamp_custom('%d/%m')}}
  {% endif %}
{% endif %}

We have an attribute called event_date in the shortcuts.calendar entity, which holds a datetime in ISO formatting.
In my instance, I want to treat start times of 00:00 as an all day event, which I don’t want to display a time for.
Lazy hack time: Because of how I’m calculating ‘This Week’ and ‘Next Week’, towards the end of the year I only display the date in a DD/MM format.

{%- set start = state_attr('shortcuts.calendar', 'event_date') %}

{%- set time = '' %}
{%- if start.time() | string != '00:00:00' %}
{%- set time = (start.time()|string)[:5] %}
{%- endif %}

{%- set event_date = start.replace(hour=0, minute=0, second=0 ) %}
{%- set diff = (event_date - today_at()).days %}
{%- if event_date.strftime('%d/%m') == '25/12' and time == '' %}
  {{- event_date.strftime('%A') }}
{%- elif diff == 0 %} {{- time }} Today
{%- elif diff == 1 %} {{- time }} Tommorrow
{%- elif diff in range(2,7) %}
  {{- time }} {{ event_date.strftime('%A') }}
{%- elif diff in range(7,15) %}
  {{- time }} {{ 'Next '~event_date.strftime('%A') }}
{%- else %}
  {{- time }} {{ event_date.strftime('%d/%m') }}
{%- endif %} 

What’s with the singling out of Christmas Day (or Christmas Eve in leap years)?

(I saw that edit, Drew :innocent: . Also note one ‘m’ in Tomorrow.)

FYI this lib handles this

Here’s an example

{% from 'easy_time.jinja' import speak_the_days %}
{{ speak_the_days('shortcuts.calendar', 'event_date') }}