Shopify(液体):查找两个日期之间的天数
Shopify (liquid): Find number of days between two dates
我不熟悉 Shopify 和 .liquid 文件语法。
我目前可以得到两个日期:
{% assign product_created_date = product.created_at | date: "%a, %b %d, %y" %}
{% assign current_date = 'now' | date: "%a, %b %d, %y" %}
这给了我当前日期以及产品的创建日期。
我想在主题中向用户显示产品发布后的日期。
我已经阅读了一些液体过滤器并进行了一些搜索,但无法准确找到自产品创建以来的天数。
我们可以使用纯流式语法来计算吗?
您可以将日期转换为代表 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>
借助 David 的回答得出
一行片段 - 考虑任何日期,例如。 '2020-04-09'
{% assign myVar="now" | date: "%s" %}{{ '2020-04-09' | date: "%s" | minus: myVar | divided_by: 3600 | divided_by: 24 | round }}
您可以 运行 并测试它 here
我不熟悉 Shopify 和 .liquid 文件语法。
我目前可以得到两个日期:
{% assign product_created_date = product.created_at | date: "%a, %b %d, %y" %}
{% assign current_date = 'now' | date: "%a, %b %d, %y" %}
这给了我当前日期以及产品的创建日期。
我想在主题中向用户显示产品发布后的日期。
我已经阅读了一些液体过滤器并进行了一些搜索,但无法准确找到自产品创建以来的天数。
我们可以使用纯流式语法来计算吗?
您可以将日期转换为代表 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>
借助 David 的回答得出 一行片段 - 考虑任何日期,例如。 '2020-04-09'
{% assign myVar="now" | date: "%s" %}{{ '2020-04-09' | date: "%s" | minus: myVar | divided_by: 3600 | divided_by: 24 | round }}
您可以 运行 并测试它 here