我想在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
---
答案 0 :(得分:6)
没有人真正回答了这个问题,但这并非不可能。
你可以得到年份之间的差异,比如自2000年以来已经过了多少年:
{{ site.time | date: '%Y' | minus:2000 }}
至于两个日期之间的日子,那就更难了。最好的办法是查看插件: https://github.com/markets/jekyll-timeago
它的输出可能有点冗长,但你可以修改插件本身(看看代码,它不是太复杂)
答案 1 :(得分: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}}
时数: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
添加更多零以添加更多小数位。
答案 2 :(得分:3)
如果你想做的只是知道你的Front Matter的日期是否早于系统时间,那么你可以使用ISO 8601日期格式并依赖词典排序。这有点作弊,但它适用于你提供的例子。
以ISO 8601格式按顺序捕获 site.time
和前方事项(以下示例中为page.past_date
和page.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 %}
给我以下输出:
currentDate:2015-07-12
PastDate早于currentDate?是的
FutureDate早于currentDate?否
答案 3 :(得分:0)
我计算了当前日期与帖子之一之间的年差,以呈现特定的消息,并且效果很好:
{% 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 %}