Search code examples
jekyllliquid

Get the difference in days between two dates in Jekyll


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

Solution

  • 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