【发布时间】:2016-09-29 17:04:00
【问题描述】:
我有一个字符串“products_2016-05-09”,其中 2016-05-09 是字符串中附加的日期。我想提取这个日期。如果日期是负 1 天,我想显示字符串“产品”。我怎样才能在流动的语法中做到这一点?
【问题讨论】:
我有一个字符串“products_2016-05-09”,其中 2016-05-09 是字符串中附加的日期。我想提取这个日期。如果日期是负 1 天,我想显示字符串“产品”。我怎样才能在流动的语法中做到这一点?
【问题讨论】:
要从string 中提取日期,请使用remove 和split 过滤器:
{% assign pdate = string | remove: "products_" %}
{% assign pdate = pdate | split: '-' %}
要检查产品日期 (pdate) 是否在 24 小时(86400 秒)内,请使用以下内容:
{% assign today = "now" | date: "%s" %}
{% assign yesterday = today | minus: 86400 %}
{% if pdate[0] == yesterday | date: "%Y" and pdate[1] == yesterday | date: "%m" and pdate[2] == yesterday | date: "%d" %}
Display string "products"
{% endif %}
注意:这仅检查产品日期是否为昨天(从现在起 24 小时前)以进行更准确的时间验证,您需要进行更多的算术运算。您也可以使用 JavaScript 在前端完成所有这些操作。
【讨论】:
以下代码对我有用:
{% assign var = {{custom_attribute.${producttype}}} %}
{% assign words = var | split: '_' %}
{% assign yestDate = 'now' | date: "%s" | minus: 86400 | date: "%F" %}
{% assign varDate = words[1] %}
{% if varDate | convert: "date" == yestDate %}
Dynamic String {{words[0]}}
{% else %}
sorry!
{% endif %}
【讨论】: