2016-05-20 93 views
1

我是Shopify和.liquid文件語法的新手。Shopify(liquid):查找兩個日期之間的天數

我可以得到兩個日期目前:

{% assign product_created_date = product.created_at | date: "%a, %b %d, %y" %} 
{% assign current_date = 'now' | date: "%a, %b %d, %y" %} 

這給了我當前的日期,並在創建該產品的日期。

我想在主題中顯示用戶,這是自產品發佈以來的日期。

我已經閱讀了一些液體過濾器,並做了一些搜索,但無法找到我將如何找到產品創建以來的日子。

我們可以使用純液體語法來計算它嗎?

回答

5

您可以將您的日期代表Number of seconds since 1970-01-01 00:00:00 UTC

{% comment %} convert our dates to Number of seconds 
       since 1970-01-01 00:00:00 UTC {% endcomment %} 
{% assign dateStart = product.created_at | date: '%s' %} 
{% assign nowTimestamp = 'now' | date: '%s' %} 

{% comment %} difference in seconds {% endcomment %} 
{% assign diffSeconds = nowTimestamp | minus: dateStart %} 

{% comment %} difference in days {% endcomment %} 
{% assign diffDays = diffSeconds | divided_by: 3600 | divided_by: 24 %} 

<p>difference in days = {{ diffDays }}</p> 
時間戳