日期比较逻辑/液体模板过滤器中 [英] Date comparison Logic / in Liquid Template Filter
问题描述
我试图创建一个类似于预购"的机制,其中Shopify液体模板的某些元素仅在当前日期大于或小于元字段中指定的日期时显示.
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 %}
但是,即使我将预购日期设置为01-01-2018,它仍然显示大于".
However, even when I set the PreOrder date to 01-01-2018 it still shows the "Is greater than".
如何正确查询?
推荐答案
您不能以这种方式比较字符串. (日期是字符串.)
You can't compare strings this way. (The dates are strings.)
您必须改用%s
日期过滤器.
You have to use the %s
date filter instead.
因此它将变为:
{% assign today_date = 'now' | date: '%s' %}
{% assign pre_date = product.metafields.Release-Date.preOrder | date: '%s' %}
{% if today_date > pre_date %}
我们使用%s
是因为它将返回当前的unix时间戳号而不是字符串.这样,您将能够比较不同的时间戳.
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.
这篇关于日期比较逻辑/液体模板过滤器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!