Petro31 / easy-time-jinja

Easy Time calculations for Home Assistant templates
MIT License
68 stars 25 forks source link

FR add support for datetimes #24

Closed Mariusthvdb closed 9 months ago

Mariusthvdb commented 10 months ago

following our Discord chat, please add support for those datetimes?

https://discord.com/channels/330944238910963714/672223497736421388/1175066825042694334

this is what we talked about:

{% from 'easy_time.jinja' import month, weekday %}
{% set date = strptime(states('sensor.afvalwijzer_next_date'),'%d-%m-%Y')|as_local %}
{{ weekday(date.isoweekday()) }} {{ date.day }} {{date.month}} {{ month(date) }} {{month(date.month)}} {{ date.year }}

also please see your suggestion to self ;-)

https://discord.com/channels/330944238910963714/672223497736421388/1175060756652367983 fwiw, I had a custom_template all-in-1

{%- macro datum(datetime, add_year=false, add_hour = false) -%}
  {%- set nu = as_timestamp(datetime) %}
  {%- set months = ['januari','februari','maart','april','mei','juni','juli',
                    'augustus','september','oktober','november','december'] -%}
  {%- set wdays = ['zondag','maandag','dinsdag','woensdag','donderdag',
                  'vrijdag','zaterdag'] -%}
  {%- set wday = nu|timestamp_custom('%w',default=none)|int(default=0) -%}
  {%- set month = nu|timestamp_custom('%m',default=none)|int(default=0) -%}
  {%- set datum = nu|timestamp_custom(wdays[wday]  ~ ' %-d ' ~ months[month-1]) -%}
  {%- set datum = datum ~ ' ' ~ datetime.year if add_year else datum -%}
  {%- set datum = datum ~ ', om ' ~ datetime.hour ~ ' uur' if add_hour else datum -%}
  {{- datum -}}
{%- endmacro -%}

allowing this https://discord.com/channels/330944238910963714/672223497736421388/1175030376046006312

{% from 'tijd.jinja' import datum %}
{{datum([ns.spring,ns.fall]|min,add_year=true, add_hour=true)}}

# non dst date times too
{{datum(strptime(states('sensor.afvalwijzer_next_date'),'%d-%m-%Y')|as_local, add_year=true, add_hour=true)}}
Petro31 commented 9 months ago

2.0.0