Liquid Date Comparison

Another silly question. I admit I am not really that strong with Liquid.

I have a custom mod that contains a custom property which is a date selector. Actually, I have several of them within the same record. Anyway, I just what to compare today’s date against the date selected in the date selector. If today’s date is before the date selected in the date selector, then display A. If today’s date is after (or past) the date selected in the date selector, then display B.

I have spent countless hours trying to solve. White flag going up. I hope this makes sense. Any ideas?

Thanks in advance!

Hi @bridge3196.
Basically, we need to convert both dates into seconds (since 1970).
Then we can compare the two values. However, the trick is that date conversion to seconds outputs a string, so to ensure Liquid is seeing the results as true numbers we need to convert them (note the | minus: 0 in the code below - by running some math on the seconds strings, Liquid will convert it to a number).

Here is a working example:

{% assign nowDateSec = "now" | date: "%s" | minus: 0 %}
{% assign modDateSec = this.yourModuleDate | date: "%s" | minus: 0 %}

{% if nowDateSec < modDateSec %}
<!-- todays date is before modules date -->
{% else %}
<!-- todays date is after modules date -->
{% endif %}

Here is some documentation on it:
https://docs.treepl.co/liquid-filters/standard-filters#secStringDate

1 Like

THANK YOU!!! Where’s the “send him a beer” button?
I spent hours today trying to figure this out. Thank you again, I really appreciate it. I need to learn to ask for help sooner.

2 Likes