1

I have a a Jekyll website and I would like to create a day counter, starting from 1 and then every day it should add +1 to the counter.

So today it would say:

Day 1

Tomorrow it should say

Day 2

The day after:

Day 3

is that possible to do in jekyll?

M5corn
  • 171
  • 5

2 Answers2

1

You just need to calculate the difference between two dates. There is some discussion about that here: https://stackoverflow.com/a/34615552/1645925

My favourite answer states:

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

I believe site.time refers to the server time that your site is hosted on but you can use 'now' to use the users time.

GeorgeButter
  • 2,521
  • 1
  • 29
  • 47
  • I think `site.time` returns the time when your site was built, and the value won't change over time. – Adam Hollett Aug 18 '17 at 00:30
  • replace your hard coded date with post.date and this is a great way to show your posts were posted 'x days ago'. – Duarte Mar 12 '23 at 18:41
0

I assume you want a web page that tells you: today is day number 16 of the month.

Because Jekyll is a static site generator, all logic will only be executed during generation of the site. When you do not update/rebuild your website (daily), the counter does not increment. I think using javascript is the only solution for this problem.

Given my assumption, the answer is: No, Jekyll does not do this. You should use javascript for this.

Mr. Hugo
  • 11,887
  • 3
  • 42
  • 60