Hello Everyone,
I am Dutch speaking and I am looking for a way to convert the date as follows: ‘01 September 2024’ to ‘01 09 2024’. I have already searched a lot, but I can’t figure it out!
Thanks for your response.
Hello Everyone,
I am Dutch speaking and I am looking for a way to convert the date as follows: ‘01 September 2024’ to ‘01 09 2024’. I have already searched a lot, but I can’t figure it out!
Thanks for your response.
Take a look to
is
{{ now().strftime('%d %m %Y') }}
report today = 23 09 2024
or with point
{{ now().strftime('%d.%m.%Y') }}
report today = 23.09.2024
Thanks for your response. What you tell me only works for “now()”, what do I do if I want to convert October, November, etc. So I mean the month is a string?
{{ strptime('01 September 2024', '%d %B %Y', '??').strftime('%d %m %Y') }}
it would be important to know if your dates are in Dutch or English. September is not going to be an issue obviously, but try the month Mei
what is the source for your dates?
Thank you very much for your response. This works, but not for all months because My date is displayed in Dutch. Is there also a solution for this?
as asked above, what s the source of your date strings? is it an integration? or some template?
it could als be the Frontend already translating a timestamp sensor to your local language…
thankyou, it’s a string in a attribute of a sensor. and it’s in Dutch
please elaborate some more, what sensor is that, and is it from an integration, or, can you set some settings regarding the output.
Ideally those date states/attributes follow the International programming conventions, like making those type timestamp. With those we could use the strftime formatting. or use various available translators.
If you can not, there probably wont be anything you can do other than create a template sensor, with a mapping of the month names
but, need more info first
Here’s a template sensor I created to make the date “pretty”, lol:
pretty_date:
unique_id: 3dSFdnfK8U
value_template: >
{% set months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] %}
{% set days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] %}
{{ days[now().weekday()] + ', ' + months[now().month-1] + ' ' + now().day | string }}
Output: Monday, September 23
You could also use a different type of array:
{% set month = 'January' %}
{% set array = { 'January':'01',
'February':'02' } %}
{{ array['January'] }}
Output: 01
Two different options based on what would work best for you.
why not use {{now().strftime('%A, %B %d')}}
?
Because I created this years ago before I knew about it … and I figured this use-case would better suit OP’s needs [esp. given they’re not trying to use now()]
haha right. well, you are in an easy situation, with only English
We Dutch need to translate those first before even being able to begin with strftime formatting.
that’s why I already suggested a mapping.
otoh we dont know about the source yet, so maybe OP can change that. Or, in case it’s a Dutch integration file a FR to optimize the source.
This converts the month name to a number:
{{ ('januari','februari','maart','april',
'mei','juni','juli','augustus',
'september','oktober','november','december')
.index(state_attr('sensor.YOUR_SENSOR','YOUR_ATTRIBUTE')|lower)+1 }}
If your attribute is really “01 September 2024” (you still haven’t shown us what is actually is), then:
{% set d,m,y = state_attr('sensor.YOUR_SENSOR','YOUR_ATTRIBUTE').split() %}
{{ '%s %02d %s' % (d,
('januari','februari','maart','april',
'mei','juni','juli','augustus',
'september','oktober','november','december')
.index(m|lower)+1, y) }}
Thanks for your response.
This works perfectly. But is it also possible that the result is "01, 02, 03, 04, etc "? If yes, how do I do this?
What is the input — exactly — and what output do you want? Do you mean:
{{ '%02d' %
('januari','februari','maart','april',
'mei','juni','juli','augustus',
'september','oktober','november','december')
.index(state_attr('sensor.YOUR_SENSOR','YOUR_ATTRIBUTE')|lower)+1 }}
You have been asked multiple times what the source of the data is — give us the entity ID, attribute name, an example of its content and what output you want. We’re wasting our time guessing.
Thank you very much, this works perfectly!
well tbh, it is not.
it might work here because it simply reformats a string now, but this is not the optimal way of manipulating timestamps in HA at all.
ideally you would have a timestamp entity or correctly formatted attribute, and re-format directly based on that.
Or even better, have the Frontend take care of it for you.
But, somehow you are not willing to show us the actual entity you are talking about, or answer our questions regarding the source of the entity.
if all you want is map a Month name to a number, ok, a mapper will do.
HA is capable of so much more though…