Get today's date in Jekyll with Liquid markup
Solution 1
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' }}
Solution 2
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' }}
Solution 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
Jeff Pratt
Senior software engineer at Proofpoint and programming language enthusiast. I'm also into philosophy, mathematics, economics, religion, history, astrophysics, music, and everything else worth studying. PHP is gross.
Updated on July 05, 2022Comments
-
Jeff Pratt almost 2 years
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? -
George Tsiokos over 9 yearsor {{ site.time | date: '%Y' }} for 2014
-
Mark Thomas over 8 yearsThis basically repeats the comment after my answer.
-
5ervant - techintel.github.io over 8 years@MarkThomas But still have the great explanation.
-
Joshua Pinter over 7 years@MarkThomas You should update your answer with the post-Ruby 1.9.3 answer.
-
Andrew M. almost 7 yearsPretty 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. -
Mark Thomas almost 7 years@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.
-
Andrew M. almost 7 yearsAgreed. 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)
-
Mark Thomas almost 7 years@menehune23 Not sure if you're implying that this workaround is any different than what the OP was trying to do.
-
Andrew M. almost 7 yearsAh, 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
-
Michael Currin about 3 yearsThis is unnecessarily verbose. If you are going to use JS, then use
Date
for an object instead ofDate.now
an integer.x = newDate(); x.getFullYear(); // 2021
-
Csaba Toth about 2 yearsLowercase
%y
would print two digit year like22
whereas uppercase%Y
would print full four digit2022