Hi everyone! I am trying to find out how to tell if a contact has a date property filled in or not. Basically we want to hide some text if this date field does not have a value. I have tried some things that work with regular text fields but do not seem to work for a date field.
EDIT: I was expecting at least one of the tests for contact A to be true. As it is an empty value.
The only difference is that I can print out the date when it exists, but if there is not value set then I still get all falses. It seems the logical checks are finding a value but nothing is printing out if there is no value set to the field.
So a date picker property is a datetime object which is a value type, so it cannot be null. Think of it like Int or String.
Without going to far into the details of this you've got 2 options:
Format the value as datetime and compare to today() formatted as datetime
Test the type of the property
See examples below:
This code:
{# Print the value using "datetimeformat" becuase we know its a datetime object #}
{{ contact.hs_lifecyclestage_customer_date|datetimeformat('%B %e, %Y') }}<br>
{# Compare the value to today #}
{{ contact.hs_lifecyclestage_customer_date == today() }}<br>
{# Format and compare the value to today #}
{{ contact.hs_lifecyclestage_customer_date|datetimeformat('%B %e, %Y') == today()|unixtimestamp|datetimeformat('%B %e, %Y') }}<br>
{# test type #}
{{ type(contact.hs_lifecyclestage_customer_date) }}<br>
Renders this output:
If its not obvious I would choose option 2 over the inevitable headache of option 1!
thank you so much @Kevin-C for your help on this. I am not sure what I am doing differently but I do not get the same results as you.
Have a "date picker" property/field we created (named "plan_effective_date"). I have set today's date in it "Jan 1, 2022" on a contact (Contact A), and nothing set on another contact (Contact B).
Contact A result (has date of today set "Jan 28, 2022")
effective_date: 1/28/22
type(effective_date): str
effective_date|datetimeformat('%B %e, %Y'):
effective_date == today(): false
effective_date pprint: (String: 1/28/22)
effective_date == '': false
effective_date == null: false
effective_date is undefined: false
effective_date is none: false
effective_date|string == '': false
If statment 'effective_date': 1/28/22
Contact B result (nothing set to the field):
effective_date:
type(effective_date): str
effective_date|datetimeformat('%B %e, %Y'):
effective_date == today(): false
effective_date pprint: (String: )
effective_date == '': false
effective_date == null: false
effective_date is undefined: false
effective_date is none: false
effective_date|string == '': false
If statment 'effective_date':
I see with Contact A that I can get "1/28/22" out but when I try to use "datetimeformat" nothing is printed out. and "effective_date == today()" comes out as False?
Some more information if this helps. This is in a custom created email template and I am testing mostly in the email preview under "actions" and selecting my test contacts. I also get the same results every time I have sent a test email to one of my test contacts.
Again thank you so much for your help on this Kevin-C
Hey @Kevin-C thank you for your reply and I ran a new test with your suggestion but it seems to be working the same way in that even when the value is empty (nothing in the date property on the contact) it still registers as true in an if statment.