Date comparison Logic / in Liquid Filter

◇◆丶佛笑我妖孽 提交于 2021-01-28 05:10:08

问题


I'm trying to add 30 days to a pre-order date and if today's date is later, display a text string and if not display another text string. Any ideas where I'm going wrong?

{% assign assign pre_date = 259200 | plus: order.created_at | date: '%s' %}
{% assign today_date = 'now' | date: '%s' %}
{% if pre_date > today_date %}
disply this
{% else %}
this
{% endif %}

回答1:


The date filter returns a string, even when you're using %s to get a number of seconds, so Shopify may be running into situations where you're comparing strings-that-look-like numbers instead of actual numbers

To coerce your variables into their proper numeric values, I find the simplest thing to do is to apply a neutral mathematical operation (either | plus: 0 or | times: 1)

So your final code might look something like:

{% assign pre_date = order.created_at | date: '%s' | plus: 259200 %}
{% assign today_date = 'now' | date: '%s' | times: 1 %}

{% if pre_date > today_date %}
  Pre-date is greater
{% else %}
  Today is the day
{% endif %}


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

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