Date comparison Logic / in Liquid Template Filter

◇◆丶佛笑我妖孽 提交于 2019-12-22 09:42:42

问题


I'm attempting to create a "Pre-Order" Like mechanic where certain elements of my Shopify Liquid Template only show if the current date is more or less than the date specified in a Metafield.

As of current this is what I have including logic:

<!-- Check Today is correct -->
<p>Today: {{'now' | date: '%d-%m-%Y' }}</p>

<!-- This is the Metafield Output as a String -->
<p>Release Date: {{ product.metafields.Release-Date.preOrder }}</p>

<!-- Assign Variable "today_date" to the current date -->
{% assign today_date = 'now' | date: '%d-%m-%Y' %}
<!-- Assign Variable "pre_date" to the string of the metafield -->
{% assign pre_date = product.metafields.Release-Date.preOrder %}
{% if today_date > pre_date %}
  Today's date is greater than PreOrder Date
{% else %}
  Today's date is not greater than PreOrder Date
{% endif %}

However, even when I set the PreOrder date to 01-01-2018 it still shows the "Is greater than".

How do I correctly query this?


回答1:


You can't compare strings this way. (The dates are strings.)

You have to use the %s date filter instead.

So it will become:

{% assign today_date = 'now' | date: '%s' %}
{% assign pre_date = product.metafields.Release-Date.preOrder | date: '%s' %}
{% if today_date > pre_date %}

We use %s because it will return the current unix timestamp number instead of a string. This way you will be able to compare the different timestamps.



来源:https://stackoverflow.com/questions/47577336/date-comparison-logic-in-liquid-template-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!