获取Jekyll两个日期之间的天数差异

Sil*_* S. 5 liquid jekyll

我想在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 %}
Run Code Online (Sandbox Code Playgroud)

在条目中有我的YAML:

---
title: ChartLine C3
type: charts
description: Chart with round for prisma
id: c3-1
date: 2015-07-18
--- 
Run Code Online (Sandbox Code Playgroud)

ent*_*oon 6

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

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

{{ site.time | date: '%Y' | minus:2000 }}
Run Code Online (Sandbox Code Playgroud)

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

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


gle*_*ech 6

在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}}
Run Code Online (Sandbox Code Playgroud)

营业时间:27780

天:1157

请注意,Liquid的divide_by操作会自动进行.

Remainder hours: {{hoursSince | modulo: 24}}
Run Code Online (Sandbox Code Playgroud)

剩余时间:12

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

{%   assign k = 10   %}
{%   assign divisor = 24   %}
{%   assign modulus = hoursSince | modulo: 24 | times: k | divided_by: divisor  %}
{{daysSince}}.{{modulus}}
Run Code Online (Sandbox Code Playgroud)

1157.5

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


Eri*_*pie 4

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

为了让这个技巧发挥作用,以 ISO 8601 格式捕获 Front Matter(以及下面的示例)中的日期 site.time和日期非常重要。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 %}
Run Code Online (Sandbox Code Playgroud)

给我以下输出:

当前日期: 2015-07-12

过去日期早于当前日期吗?是的

FutureDate 早于 currentDate?不

  • 你好,我不明白为什么 futureDate 是必要的。你能给我多解释一下吗?我只想知道过去 10 天内创建的页面。 (2认同)