【问题标题】:Get the difference in days between two dates in Jekyll获取 Jekyll 中两个日期之间的天数差异
【发布时间】:2015-07-10 11:48:49
【问题描述】:

我想获取 Jekyll 中两个日期之间的天数差异。我怎样才能做到这一点?

{% capture currentDate %}{{ site.time | date: '%Y-%m-%d' }}{% endcapture %}
{{currentDate}}
{% capture event_date %}{{ entry.date }}{% endcapture %}
{% if event_date < currentDate %}Yes{% else %}No{% endif %}

在条目中有我的 YAML:

---
title: ChartLine C3
type: charts
description: Chart with round for prisma
id: c3-1
date: 2015-07-18
--- 

【问题讨论】:

    标签: jekyll liquid


    【解决方案1】:

    在 Liquid(Jekyll 的模板引擎)中执行此操作的方法很愚蠢:

    {%   assign today = site.time | date: '%s'      %}
    {%   assign start = '20-01-2014 04:00:00' | date: '%s'  %}
    {%   assign secondsSince = today | minus: start     %}
    {%   assign hoursSince = secondsSince | divided_by: 60 | divided_by: 60     %}
    {%   assign daysSince = hoursSince | divided_by: 24  %}
    
    Hours: {{hoursSince}}
    Days: {{daysSince}}
    

    小时:27780

    天数:1157

    请注意,Liquid 的 divide_by 操作会自动舍入。

    Remainder hours: {{hoursSince | modulo: 24}}
    

    剩余时间:12

    如果这让你和我一样烦恼,那么你可以这样做来恢复小数位:

    {%   assign k = 10   %}
    {%   assign divisor = 24   %}
    {%   assign modulus = hoursSince | modulo: 24 | times: k | divided_by: divisor  %}
    {{daysSince}}.{{modulus}}
    

    1157.5

    k 添加更多零以添加更多小数位。

    【讨论】:

    • 我很惊讶为什么这不是公认的答案。无论如何,我要 +1。
    【解决方案2】:

    实际上没有人回答这个问题,但这并非不可能。

    你可以得到年份之间的差异,比如自 2000 年以来已经过去了多少年:

    {{ site.time | date: '%Y' | minus:2000 }}
    

    至于两个日期之间的天数,那就更难了.. 最好的办法是查看插件: https://github.com/markets/jekyll-timeago

    虽然它的输出可能有点冗长,但你可以修改插件本身(看看代码,它不是太复杂)

    【讨论】:

      【解决方案3】:

      如果您只想知道 Front Matter 中的日期是否早于系统时间,那么您可以使用 ISO 8601 日期格式并依赖字典顺序。这有点作弊,但它适用于您提供的示例。

      以 ISO 8601 格式捕获site.time 和 Front Matter 中的日期(下例中的page.past_datepage.future_date)非常重要,这样这个技巧才能发挥作用。

      ---
      layout: default
      past_date: 2015-03-02
      future_date: 2016-03-02
      ---
      
      {% capture currentDate %}{{ site.time | date: '%F' }}{% endcapture %}
      {% capture pastDate %}{{ page.past_date | date: '%F' }}{% endcapture %}
      {% capture futureDate %}{{ page.future_date | date: '%F' }}{% endcapture %}
      <br>currentDate: {{currentDate}}
      <br>PastDate earlier than currentDate? {% if pastDate < currentDate %}Yes{% else %}No{% endif %}
      <br>FutureDate earlier than currentDate? {% if futureDate < currentDate %}Yes{% else %}No{% endif %}
      

      给我以下输出:

      当前日期:2015-07-12

      PastDate 早于 currentDate?是的

      FutureDate 早于 currentDate?没有

      【讨论】:

      • 您好,我不明白为什么futureDate 是必要的。你能再解释一下吗?我想知道过去 10 天内创建的页面。
      • @SilvioS。没必要,我只是用它作为另一个例子来证明比较有效。 ;)
      【解决方案4】:

      我计算了当前日期和帖子中的年份之间的差异来呈现特定的消息,它工作得非常好:

      {% assign currentYear = site.time | date: '%Y' %}
      {% assign postYear = post.date | date: '%Y' %}
      {% assign postAge = currentYear | minus: postYear | minus: 1 %}
      
      {% if postAge >= 3 %}
      <div class="maybe-obsolete">
        This post has been published more than {{ postAge }} years ago, and parts
        of its contents are very likely to be obsolete by now.
      </div>
      {% endif %}
      

      【讨论】:

        猜你喜欢
        • 2017-10-24
        • 2021-11-15
        • 1970-01-01
        • 2011-03-20
        • 1970-01-01
        • 2011-03-19
        • 1970-01-01
        • 1970-01-01
        • 2019-05-11
        相关资源
        最近更新 更多