Get the difference in days between two dates in Jekyll

风格不统一 提交于 2020-01-01 05:41:15

问题


I would like to get the difference in days between two dates in Jekyll. How can I achieve this?

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

In the entry there is my YAML:

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

回答1:


If all you want to do is know whether a date from your Front Matter is earlier than the system time then you can use the ISO 8601 date format and rely on the lexicographical ordering. It's sort of cheating but it'll work for the example you provided.

It's important to capture both site.time and the date from your Front Matter (page.past_date and page.future_date in the example below) in ISO 8601 format in order for this trick to work.

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

Gives me the following output:

currentDate: 2015-07-12

PastDate earlier than currentDate? Yes

FutureDate earlier than currentDate? No




回答2:


No-one actually answered the question but it's not impossible.

You can get the difference between years, say how many years have passed since the year 2000 would be:

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

As for days between two dates, that's harder.. best bet is to look at the plugin: https://github.com/markets/jekyll-timeago

It's output might be a bit verbose though but you could modify the plugin itself (have a look through the code, it's not too complex)




回答3:


The way to do this in Liquid (Jekyll's templating engine) is silly:

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

Hours: 27780

Days: 1157

Note that Liquid's divide_by operation rounds automatically.

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

Remainder hours: 12

If this annoys you as much as it annoyed me then you can do this to recover decimal places:

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

1157.5

Add more zeroes to k to add more decimal places.




回答4:


I computed the difference in years between current date and the one of the post to render a specific message, and it works pretty nicely:

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


来源:https://stackoverflow.com/questions/31340018/get-the-difference-in-days-between-two-dates-in-jekyll

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!