CMS Development

SteveHTM
Top Contributor | Partner
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:

Screen Shot 2019-04-05 at 10.33.01 AM.png

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:

Screen Shot 2019-04-05 at 11.19.40 AM.png

So the question is - how can I overcome the seeming fragility of the use of this filter with strings that are not literal constants?

 

Grateful for any help here.

 

Steve

Steve Christian

HTM Solutions

https://info.htmsolutions.biz/meetings/stevec2

mobilePhone
+1 6195183009
emailAddress
stevec@htmsolutions.biz
website
www.htmsolutions.biz
address
San Diego, CA
Create Your Own Free Signature
4 Replies 4
PDeT
Participant

Looking for wisdom on usage of "strtotime" filter

This is still broken, 4years later 😞

0 Upvotes
JGODEFROY
Member

Looking for wisdom on usage of "strtotime" filter

Hello @PDeT 

I just add the similar issue as yours, and I fixed it using this thread : https://community.hubspot.com/t5/CMS-Development/How-to-convert-date-string-dd-MM-yy-to-date-object-...

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. 

I'm also facing a deadend here... Please help. 

0 Upvotes
Merb01
Participant

Looking for wisdom on usage of "strtotime" filter

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!

0 Upvotes
lscanlan
HubSpot Alumni
HubSpot Alumni

Looking for wisdom on usage of "strtotime" filter

Hi @SteveHTM,

 

I've been looking at this today. I think based on our documentation of |strtotime() here: https://designers.hubspot.com/docs/hubl/hubl-supported-filters#strtotime, that any date and time pattern string (as defined here: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) would be an acceptable format to convert into a datetime object. So for example I should be able to do something like this:

 

{% 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.

 

 - Leland

Leland Scanlan

HubSpot Developer Support