【问题标题】:How to output days of week as a date then change every week in liquid如何将星期几输出为日期,然后每周在液体中更改
【发布时间】:2021-11-19 16:29:27
【问题描述】:

我正在尝试在 Liquid (Shopify) 中输出一个字符串,该字符串显示该特定周的周四、周五、周六和周日的日期,然后每周更新该周的正确日期。

例如,“立即订购,28 日、29 日、30 日和 31 日取货。”

我不认为这是一种非常有用的方法,而且我不确定如何在一周内循环使用它而不创建一个大数组。除非那是唯一的方法?

{% assign tday = "today" | date: "%a" %}
{% assign dtime = 4 | times: 86400 %}
{% assign dday = {{ tday | plus: dtime | date: "%a"}} %}

{% if “today” == ‘Mon’ %}
    {{% ORDER NOW FOR COLLECTION ON {{dday}} %}}

{% assign tday = "today" | date: "%a" %}
{% assign dtime = 3 | times: 86400 %}
{% assign dday = {{ tday | plus: dtime | date: "%a"}} %}

{% elseif “today” == ‘Tue’ %}
    {{% ORDER NOW FOR COLLECTION ON {{dday}} %}}

{% assign tday = "today" | date: "%a" %}
{% assign dtime = 2 | times: 86400 %}
{% assign dday = {{ tday | plus: dtime | date: "%a"}} %}

{% elseif “today” == ‘Wed’ %}
    {{% ORDER NOW FOR COLLECTION ON {{dday}} %}}

【问题讨论】:

    标签: shopify liquid


    【解决方案1】:

    当您只使用一种时间格式时,这很容易。 在我的方法中,我只使用时间戳。

    首先我得到当天的数字,星期六是6。

    当前日期变量为负 1,因为大多数格式都适合计算从 0 到 6(如数组)的天数,而不是从 1 到 7。

    在那之后,我正在做一个计算以获得星期一的时间戳。

    一天中的秒数乘以当天的当前数字,我将其乘以 -1 以得到负数,当我将当前日期加到该数字上时,结果将是星期一。

    86400*5=518400
    518400*-1=-518400 // Negative 5 days
    -518400+1637441515...
    

    我们会得到星期一的时间戳。

    Monday 变量对您来说可能有点混乱,但我尝试只使用一行来完成所有计算。

    最后,我通过将天的秒数添加到星期一时间戳来进行计算。

    代码如下:

    {%- assign day_in_seconds = 86400 -%}
    {%- assign time = 'now' | date: '%s' | plus: 0 -%}
    {%- assign current_day = time | date: '%u' | minus: 1 -%}
    {%- assign monday = day_in_seconds | times: current_day | times: -1 | plus: time -%}
    
    {%- for i in (3..6) -%}
        {%- assign day = day_in_seconds | times: i | plus: monday  -%}
        
    
        {{- day | date: "%a, %b %d, %y" -}}
        <br />
    
    {%- endfor -%}
    

    该示例以星期六为一天,但它们与一周相关。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2012-04-27
      • 1970-01-01
      • 2012-09-26
      • 2021-12-08
      • 2013-01-09
      • 2011-01-29
      相关资源
      最近更新 更多