Apr 5, 20192:26 PM - edited Apr 18, 201910:17 AM
Top Contributor | Partner
Looking for wisdom on usage of "strtotime" filter
I'm wrestling with what seem to be limitations or at least knowledge gaps regarding manipulation of strings into time/date values. I'm trying to resolve this down to two which I'm sure some experts here can easily solve:
Q1) The documentation of the "strtotime" filter suggests that the format of the source string to be converted can be flexible using modification of the format string parameter, but the only example given is a fully qualified date/time/timezone format something like:
{% set new_date = "2018-07-14T14:31:30+0530"|strtotime("yyyy-MM-dd'T'HH:mm:ssZ %}<code></code>
I've been attempting to use different format string options to the filter and repeatedly get Design Manager errors thrown up such as displayed below:
So the question is - what format strings would it be possible to use with this filter?
Q2) If the filter only works with one format, I would like to concatenate a simpler date only string with the required (but redundant for my purposes) time/timezone padding:
{% set date_text = "2019-04-01" %}
{% set dummy_time_text = "T00:00:00+0500" %}
{% set full_date_text = date_text ~ dummy_time_text %}
{{ full_date_text|pprint }}
{% set full_date = full_date_text|strtotime("yyyy-MM-dd'T'HH:mm:ssZ") %}
{{ full_date|pprint }}
The concatenation of strings to a local variable seems to work as expected.
But when I try and use the concatenated string with the conversion filter, I get an error thrown by the Design Manager again:
So the question is - how can I overcome the seeming fragility of the use of this filter with strings that are not literal constants?
Indeed it works well when using a defined date in the inital variable.
On the other hand I am still facing a issue when trying to fetch a Date from my contact Object.
Although the props says my data is supposed to be a datetime when I pprint it it comes out as a string. The Split function does not work on the string though so unable to perfom the trick in the thread provided.
Hi @lscanlan I am seeing the same thing I believe. We have a custom property created that is a date picker. When I pick "May 1st, 2022" in a test contact on that field it renders in a template as "5/1/22". We want to display this in a pretty way. When I "pprint" it gives "(String: 3/1/22)". So since I am working with a string I eventually came to the "strtotime" filter in the docs and tried:
{% set effective_date = contact.plan_effective_date %}
<p>{{ effective_date|strtotime("M/d/yy") }}</p>
So far I have tried a few date match formating patterns and none of them have worked so far. I see the error: "com.hubspot.jinjava.interpret.InterpretException: stringToTime() could not match datetime input 5/1/22 with datetime format M/d/yy"
Kinda stuck =(
Any insite into this would be very helpful thank you all!
{% set new_date = "1978-06-19" %}
{{ new_date|strtotime("yyyy-MM-dd") }}
...but I'm seeing the same error that you are. I've reached out to some of our engineers to see if I'm understanding the functionality correctly and if this is working how we'd expect it to. I'll update you when I know a little more.