More or less than on dates

Does anyone know if it’s possible to run an if statement based on a date in hubdb?

I have a set of dates in a column which pulls through fine, but when trying to amend it with an if statement, it doesn’t work!
I am trying to display 1 of 2 items depending on the date. e.g. if it’s before 1 March 2021 then show A content else show B content
{% elif row[“anticipated-build-completion”] | datetimeformat(‘%e %B %Y’) > ‘1 March 2021’ %}
Show A content
{% else %}
Show B content
{% endif %}
What am I doing wrong?

@ben-duchy the problem is because you are formatting it, then trying to use math/logic on a formatted date. Instead, you should try using the unixtimestamp equivalant for the date. This way you are just comparing two numbers with each other so the computer can understand what you’re doing… so something like this:

{% if unixtimestamp(row.datetimeobject) < 1614556800 %}
I'm a date before March 1st 2021!
{% endif %}

Hi @stefen, great idea, I wasn’t even aware of this method.

Unfortunately I can’t get it to work correctly as its pulling in all rows to show ‘A content’ or ‘B content’ depending on the less / more arrow used. It’s not filtering them!
Do I need to do anything different in HubDB?

Hey @ben-duchy

Looks like you open you if statement with an else if rather than an if. try this:

{% if row["anticipated-build-completion"] | datetimeformat('%e %B %Y') > '1 March 2021' %}
Show A content
{% else %}
Show B content
{% endif %}

Hi @Kevin-C, thanks for your reply. It’s part of a wider condition - I forgot to mention that.

Hey @ben-duchy

Ahh gotcha.

I wonder if the mutating/formatting of the data is getting in the way.

As a best practice we try to avoid mutating data until the last possible time (usually displaying it). We do this because mutating data back and forth can make it hard to track and understand the state of the data. Also when comparing a dynamic value with a static value we try to match the static value’s format to that of the dynamic.

To test if this might be the case try printing the variable and comparing manually:

{{ row["anticipated-build-completion"] }}

Basically take a look at that output and then try to modify the static date format you’re checking against.

This should make the bug easier to assess.

EDIT

Im working through a test now will follow up soon

Hey again @ben-duchy

Try this:

{% set staticDate = "2021-03-01T00:00:00+0000"|strtotime("yyyy-MM-dd'T'HH:mm:ssZ") %}{# the date you're checking against #}
{% set dynmDate = row["anticipated-build-completion"] %} {# your dynamic date #}

{% if dynmDate|between_times(staticDate, 'days') >= 1 %} {# Check using between times #}

 {# if the date is before teh staticDate #}

 {% elif %}

 {# if the date is after teh staticDate #}

{% endif %}

Thanks @Kevin-C thats now working, although I had to swap out ‘elif’ for ‘else’ (not sure why that would make a difference).