73

This (should) be easy, I think, but I'm unable to get today's date to show in a Jekyll page using Liquid markup. According to the documentation, I should be able to do this to get this date's year:

{{ 'now' | date: "%Y" }}

But all that gets rendered is the string now, not any formatted date. What am I doing wrong?

Alan W. Smith
  • 24,647
  • 4
  • 70
  • 96
Jeff Pratt
  • 1,619
  • 1
  • 13
  • 21

4 Answers4

137

It didn't work for me either. It appears you've hit a current bug in the Ruby 1.9.3 support. There is a pull request that fixes the bug, but it's not incorporated yet. A workaround is listed, perhaps it will work for you:

{{ site.time | date: '%y' }}
Mark Thomas
  • 37,131
  • 11
  • 74
  • 101
  • 39
    or {{ site.time | date: '%Y' }} for 2014 – George Tsiokos Nov 29 '14 at 21:10
  • 3
    Pretty sure this only gets the time from the last time you ran the `jekyll` command. Theoretically, if you don't update your site much, this could get stale. All in all, it's not the same as getting the current time. – Andrew M. Jul 16 '17 at 05:48
  • @menehune23 The point is moot anyway. The fix has been incorporated into jekyll, so the OP's code works and there is no need for this workaround anymore. – Mark Thomas Jul 16 '17 at 23:43
  • Agreed. Just wanted to point it out for those who don't know that it's in there now (and who haven't tried it for themselves) – Andrew M. Jul 17 '17 at 03:18
  • @menehune23 Not sure if you're implying that this workaround is any different than what the OP was trying to do. – Mark Thomas Jul 25 '17 at 12:24
  • Ah, so it seems both approaches only capture the time at build, not the time of access. Guess that makes sense in thinking about it, because you'd need JavaScript for the latter – Andrew M. Jul 25 '17 at 16:24
  • Lowercase `%y` would print two digit year like `22` whereas uppercase `%Y` would print full four digit `2022` – Csaba Toth Mar 18 '22 at 18:06
9

To get the whole year, for example "2015", from the site.time, you can either use:

{{ site.time | date: '%Y' }}
# OR
20{{ site.time | date: '%y' }}

To just get the last 2 digits from the year 2015, this will just output "15":

{{ site.time | date: '%y' }}
3

Perhaps the question title is misleading but I actually wanted today's date and not the year. This works for me:

{{ site.time | date: '%B %d, %Y' }}

Today it produced: January 04, 2019

MobileMateo
  • 424
  • 5
  • 4
0

{{ site.time }} represent the time the site did get updated it is a fixed date not a dynamic one.

Such request require Javascript with Date.getTime() or Date.now() example:

<script>document.write(Math.round(Date.now() / (365 * 24 * 60 * 60 * 1000) + 1970 - 1));</script>
intika
  • 8,448
  • 5
  • 36
  • 55
  • This is unnecessarily verbose. If you are going to use JS, then use `Date` for an object instead of `Date.now` an integer. `x = newDate(); x.getFullYear(); // 2021` – Michael Currin Apr 19 '21 at 08:38