Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the difference in days between two dates in Jekyll

Tags:

jekyll

liquid

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
--- 
like image 600
Silvio S. Avatar asked Jul 10 '15 11:07

Silvio S.


People also ask

How do you subtract two dates in darts?

How do you subtract dates in darts? subtract method DateTime today = new DateTime. now(); DateTime fiftyDaysAgo = today. subtract(new Duration(days: 50)); Notice that the duration being subtracted is actually 50 * 24 * 60 * 60 seconds.

How do I count days between two dates in jquery?

getTime()- endDay. getTime() to determine the time difference between two dates. After that, we will determine the number of days between dates. We will do this by dividing the number of milliseconds in a day and the time difference of both dates.


2 Answers

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.

like image 158
gleech Avatar answered Oct 19 '22 08:10

gleech


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)

like image 8
entozoon Avatar answered Oct 19 '22 08:10

entozoon