2016-09-29 83 views
1

我有一個字符串「products_2016-05-09」,其中2016-05-09是日期附加在字符串中。我想提取這個日期。如果日期是零下1天,我想顯示字符串「產品」。我怎樣才能用液體語法來做到這一點?提取字符串並與日期進行比較

回答

0

取出從string日期,使用removesplit過濾器:

{% 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在前端完成所有這些操作。

+0

「產品」 部分僅僅是一個例子。該解決方案對我無效。 –

0

下面的代碼爲我工作:在字符串

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