【问题标题】:Date comparison Logic / in Liquid Template Filter日期比较逻辑 / 在 Liquid 模板过滤器中
【发布时间】:2018-05-14 14:40:21
【问题描述】:

我正在尝试创建类似“预购”的机制,其中我的 Shopify Liquid 模板的某些元素仅在当前日期多于或少于元字段中指定的日期时显示。

截至目前,这是我所拥有的,包括逻辑:

<!-- 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 %}

但是,即使我将 PreOrder 日期设置为 01-01-2018,它仍然显示“大于”。

如何正确查询?

【问题讨论】:

    标签: date shopify liquid liquid-layout


    【解决方案1】:

    您不能以这种方式比较字符串。 (日期是字符串。)

    您必须改用%s 日期过滤器。

    所以会变成:

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

    我们使用%s,因为它会返回当前的unix时间戳编号而不是字符串。这样您就可以比较不同的时间戳。

    【讨论】:

    • 这很完美,很有意义,我非常感谢您的解释:-)
    • 请注意,您可能需要将其转换为数字以进行某些日期比较,因此我建议将 date: '%s' | times: 1 用于这两个分配
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 2018-09-25
    相关资源
    最近更新 更多