CMS Development

Merb01
Participant

How to render a pretty date

Goal, trying to display a custom contact property that is a date in a pretty format. We created the property as a date picker and named it "plan_effective_date". Ill walk you through my thought process on this and please let me know what I am missing.

 

I found that the field is a string type using the "pprint" filter. So I started down the rabbit hole of finding some helper function that can format the date. I found "datetimeformat" but that can only accept a date object.

 

So I tried converting this string into a date object using "strtotime" filter. When I put "May 1, 2022" for example, in "plan_effective_date" it prints out as "5/1/22"

 

{% set effective_date = contact.plan_effective_date %} {# pprint = "(String: 5/1/22)" #}
{{ effective_date|strtotime("M/d/y") }}

{# Gives error:
com.hubspot.jinjava.interpret.InterpretException: stringToTime() could not match datetime input 5/1/22 with datetime format M/d/yy
#}

 

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"

So how can I get past this first hudle of turning a string date into a date object? Any help is much appreciated!

 

0 Upvotes
Merb01
Participant

How to render a pretty date

Hey @Kevin-C yes I have. This is what I currently have:

{% set effective_date = contact.plan_effective_date %}
<p>{{ effective_date|pprint }}</p>
<p>test: {{ effective_date|datetimeformat('%B %e, %Y') }}</p>
{% if effective_date %}
    <h2>Your Effective date is {{ effective_date }}</h2>
{% endif %}

 

And this is what it renders as:

Screenshot from 2022-01-20 10-46-46.png

 

Thank you for the suggestion though! Makes me feel a little better I am not the only one to think of that

0 Upvotes
Kevin-C
Recognized Expert | Partner
Recognized Expert | Partner

How to render a pretty date

Hey @Merb01 

I miss read. See this documentation, below the example has a dtable of all of the directives you can use to format the date as you would like.

("%b %e,%Y")

This might be what you're looking for.

0 Upvotes
Merb01
Participant

How to render a pretty date

It looks lik the only directive that you changed was from the "%B" to "%b" and both have the same result. Nothing is printed. I was expecting to see the date after the "test: " from my screenshot. But nothing is displaying. I do see the string of "5/1/22" when I just put out "{{ contact.plan_effective_date }}" So that tells me that there is data there.

{{ contact.plan_effective_date }}

 

0 Upvotes
Kevin-C
Recognized Expert | Partner
Recognized Expert | Partner

How to render a pretty date

 @Hey @Merb01 

 

Have you tried something like this:

{{ contact.plan_effective_date|datetimeformat('%B %e, %Y') }} 
0 Upvotes
Merb01
Participant

How to render a pretty date

I did see this post that seems to be a similar issue to mine. Anyone see a way around this?

https://community.hubspot.com/t5/CMS-Development/Looking-for-wisdom-on-usage-of-quot-strtotime-quot-...

0 Upvotes