在杰基尔计算时间

时间:2015-10-01 18:22:21

标签: datetime jekyll date-arithmetic

我在基于jekyll的网站上渲染帖子的时间戳,如下所示:

{{ post.date | date: "%Y%m%dT%H%M" }}00Z

我现在尝试像这样呈现post.date + 3hours

{{ post.date + 3/24 | date: "%Y%m%dT%H%M" }}00Z

虽然jekyll似乎解析这个很好,但结果根本没有改变(仍然显示post.date而不是post.date + 3hours

那么如何将3小时添加到post.date

注意:这是为事件创建ics文件所以post.date是开始日期和时间,结束日期和时间是3小时后。

1 个答案:

答案 0 :(得分:2)

我们的想法是在日期时间戳(page.date | date: '%s')上添加秒数。

_includes / schedule.html 是自我解释:

{% comment %}+++++++++++++++++++++++++++++++++++++++
this include can be called with or without vars

hoursToAdd : int number of hours to add
minutesToAdd : int number of minutes to add

// include with no vars - default duration is applied
{% include schedule.html %}

// include with vars
{% include schedule.html hoursToAdd=1 minutesToAdd=30 %}
or
{% include schedule.html minutesToAdd=35 %}
+++++++++++++++++++++++++++++++++++++{% endcomment %}

{% assign defaultDuration = 3 %}

{% if include.hoursToAdd %}
    {% assign hoursToSec = include.hoursToAdd | times: 3600 %}
{% else %}
    {% assign hoursToSec = defaultDuration | times: 3600 %}
{% endif %}

{% if include.minutesToAdd %}
    {% assign minutesToSec = include.minutesToAdd | times: 60 %}
{% else %}
    {% assign aminutesToSec = 0 %}
{% endif %}

{% comment %} ++ Compute endTime : translate to timestamp and add second ++{% endcomment %}
{% assign endTime = page.date | date: '%s' | plus: hoursToSec | plus: minutesToSec %}

{% comment %} ++ Output ++{% endcomment %}
<p>
  from {{ page.date | date: "%Y%m%dT%H%M" }}00Z 
  to {{ endTime | date: "%Y%m%dT%H%M" }}00Z
</p>