How to do time manipulation on a string that contains only hours and minutes?

Hi all,

I’ve got two entities, one that contains a string which is ultimately a time, but its represented simply as “4:30 PM” (the time a ferry leaves). I’ve got another entity from the google maps travel time tracker, who’s value is merely ‘35’ (minutes, but the value is just straight 35)

I’m trying to calculate the time the ferry leaves, minus the trip time, to essentially tell me when i need to leave by.

The trouble is, using date and time functions, these values are not interpreted properly as timestamps of course, and even trying to convert them to one with strptime() etc will fail, because they dont contain a date, only hours and minutes, or minutes.

Anyone have suggestions on how I might subtract ‘35’ from '4:30 PM" in this example, taking into account that it is in fact a time?

{% set t1 = '4:30 PM' %}
{% set t2 = '35' %}
{{ today_at(strptime(t1, '%I:%M %p').timestamp() | timestamp_custom('%H:%M')) - timedelta(minutes = t2 | int(0)) }}

Had the time been available in 24-hour format, as opposed to 12-hour format, the template would have been simpler:

{% set t1 = '16:30' %}
{% set t2 = '35' %}
{{ today_at(t1) - timedelta(minutes = t2 | int(0)) }}

Amazing, thank you! I wasnt aware of today_at() or timedelta()

This works great.

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic is resolved. This helps other users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.