2015-07-10 69 views
4

我想要了解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 %} 

在入门有我的YAML:

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

回答

2

如果你想要做的是知道,从你的接待事项的日期是否比系统时间之前,那么你可以使用ISO 8601日期格式并依赖于字典顺序。这有点欺骗,但它会适用于您提供的示例。

它来捕捉site.time和你前面的物质(在下面的例子中page.past_datepage.future_date)在ISO 8601的格式,以便在日期这一招工作是很重要的。

--- 
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年7月12日

PastDate:

的currentdate?是

FutureDate早于currentDate吗?没有

+1

你好,我不明白为什么futureDate是必要的。你能解释我多一点吗?我想知道过去10天内创建的页面。 –

+0

@SilvioS。没有必要,我只是用它作为另一个例子来表明比较是有效的。 ;) –

4

没有人真的回答了这个问题,但这不是不可能的。

你可以得到岁之间的区别,说了多少年过去了,因为2000年将是:

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

至于两个日期之间的天,这是很难..最好的办法是看插件: https://github.com/markets/jekyll-timeago

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

3

在液体做到这一点的方式(哲基尔的模板引擎)是愚蠢的:

{% 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

注意,液体的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添加更多零以增加更多小数位数。