Craig D
Craig D

Reputation: 13

Date comparison Logic / in Liquid Filter

I'm trying to add 30 days to a pre-order date and if today's date is later, display a text string and if not display another text string. Any ideas where I'm going wrong?

{% assign assign pre_date = 259200 | plus: order.created_at | date: '%s' %}
{% assign today_date = 'now' | date: '%s' %}
{% if pre_date > today_date %}
disply this
{% else %}
this
{% endif %}

Upvotes: 1

Views: 2288

Answers (2)

JCQ50
JCQ50

Reputation: 47

Use "abs" filter. abs = absolute value. No math filters needed.

Upvotes: 0

Dave B
Dave B

Reputation: 3248

The date filter returns a string, even when you're using %s to get a number of seconds, so Shopify may be running into situations where you're comparing strings-that-look-like numbers instead of actual numbers

To coerce your variables into their proper numeric values, I find the simplest thing to do is to apply a neutral mathematical operation (either | plus: 0 or | times: 1)

So your final code might look something like:

{% assign pre_date = order.created_at | date: '%s' | plus: 259200 %}
{% assign today_date = 'now' | date: '%s' | times: 1 %}

{% if pre_date > today_date %}
  Pre-date is greater
{% else %}
  Today is the day
{% endif %}

Upvotes: 1

Related Questions