Date Comparison Issues in Liquid

Hi everyone. I’ve been banging my head against the wall trying to put together a liquid script that completes the following:

  • assigns site.time to a variable and uses yyyy-MM-dd format, removing whitespaces and dashes and completing a mathematical function to ensure it is handled like an integer.
  • assign one of the properties of the data.yml file hashes (valid_start), removing whitespaces and dashes and completing a mathematical function to ensure it is handled as an integer. This property uses the string date format yyyy-MM-dd (example: 2024-04-30).
  • use a where_exp to filter out all items in the data.yml whose valid_start occurs in the future.

I’ve essentially tried everything proposed in previous threads, and am even using the very clever to_date plugin, but could really use another set of eyes here.

This is the last state of my script:

{% assign current_date = site.time | to_date | remove: '-' | remove: ' ' | plus: 0 %}
{% assign wnv_display_start = item.valid_start | remove: '-' | remove: ' '| plus: 0 %}
{% if site.data.wnv[0] %}
{%- for item in site.data.wnv | where_exp: 'item', 'wnv_display_start <= current_date' -%}

Thanks in advance for anyone who has the time and capacity to have a look.

what happens when you do that? an error? or it just doesn’t show anything?

If you output each value what do you get?

Also keep in mind site.time is a one time thing at build time I think.

In the for loop your where is comparing the 2 assigned values but I think it is actually look for those to be attributes of the item in site.data.wnv? maybe your assigns need to be in the loop? right, your wnv_display_start is trying to get the value of the item before the loop with the item even runs. I think you need to remove the where and move the assign down into the loop and then do an if? but output the values along the way so you can see what is happening.

1 Like

@rdyar, you don’t miss! Your proposed solution worked like a charm.

But let me talk through your questions to see if we can make this thread more useful to others.

what happens when you do that? an error? or it just doesn’t show anything?

So what was happening previously was…nothing. The script was running without error, but items with future dates were also being included in the output. I think you’re assumption here is spot-on: the assigns need to be in the loop, otherwise they are left aside when it begins.

So this is how the script, based on your suggestion, looks now (you’ll have to imagine the closing end):

{% if site.data.wnv[0] %}
{%- for item in site.data.wnv -%}
{% assign current_date = site.time | to_date | remove: '-' | remove: ' ' | plus: 0 %}
{% assign wnv_display_start = item.valid_start | remove: '-' | remove: ' '| plus: 0 %}
{% if wnv_display_start <= current_date %}

This achieves exactly what I was hoping for.

Thanks again, @rdyar, for your helping me along here. Truly appreciated!

1 Like