日期比较逻辑/液体过滤器中 [英] Date comparison Logic / in Liquid Filter
问题描述
我正在尝试将30天添加到预订日期中,如果今天的日期较晚,请显示一个文本字符串,如果不显示其他文本字符串.有什么想法我要去哪里吗?
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 %}
推荐答案
即使您使用%s
来获取数字, date
过滤器也会返回一个字符串几秒钟的时间,因此Shopify可能会遇到您正在比较看起来像字符串的数字而不是实际数字的情况
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
要将变量强制为正确的数值,我发现最简单的方法是应用中性的数学运算( |加号:0
或 |次数:1 代码>)
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 %}
这篇关于日期比较逻辑/液体过滤器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!