Shopify(液体):查找两个日期之间的天数

时间:2016-05-20 07:36:33

标签: 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" %}

它给出了当前日期以及产品创建日期。

我想在主题中显示用户,即产品发布后的日期。

我已经阅读了一些液体过滤器并进行了一些搜索,但无法确切了解自产品创建以来的日期。

我们可以使用纯粹的液体语法来计算吗?

2 个答案:

答案 0 :(得分:7)

您可以将日期转换为代表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>

答案 1 :(得分:1)

在David's Answer的帮助下得出 一行摘要-考虑任何日期,例如。 '2020-04-09'

{% assign myVar="now" | date: "%s" %}{{ '2020-04-09' | date: "%s" | minus: myVar | divided_by: 3600 | divided_by: 24 | round }}

您可以运行并对其进行测试here

相关问题