Timestamp filter issue

I have an issue with a timestamp filter:

{{ Tijdstip | timestamp_custom('%H%M') }}

It’s also returning seconds. How can I solve this?


{{ as_timestamp(Tijdstip) | timestamp_custom('%H%M') }}

What is Tijdstip? If it is not a numeric timestamp, then timestamp_custom will just pass it through untouched.

Tijdstip is a variable passed by Dialogflow.
It is passed as a timestamp inside json.

as_timestamp() gives me ‘None’ as return.

I would not suggest using as_timestamp() if you already have a timestamp (in some form.) Can you tell use exactly what Tijdstip looks like? Is it a number? Is it a string representation of a number? Is it a string representation of a date and/or time? Is it JSON that contains a field that contains a timestamp. Your descriptions are too vague.

the returned value is for example 14:00:00 for 2pm.
I’d just want to trim the seconds off.

So it’s a string in the format of HH:MM:SS? Ok, then that’s easy:

{{ Tijdstip[:-3] }}

Where I got confused was by you using the term “timestamp”. That usually means a “Unix time”, which is the number of seconds since the “epoch.” E.g., what {{ as_timestamp(now()) }} would return.

Could you expand on why you wouldn’t suggest using as_timestamp? I’d love to know more about it’s intended functionality. I mentioned it because I’ve had success in the past when trying to use the timestamp filter.

Only because as_timestamp requires a string that contains date and time and which meets certain formatting requirements. In this case the variable (Tijdstip) was not that, which results in as_timestamp returning None.

1 Like
{{ Tijdstip[:-3] }}

Is working, thanks.
To continue on this discussion: Is there a way to tell HAS that the incoming value is a time string?
For my script, trimming the string is just fine. But what if I want to calculate with it?

There are several time related functions and filters you can use in Jinja. See here.