Get today's date in Jekyll with Liquid markup
Asked Answered
R

4

74

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?

Rucker answered 17/9, 2012 at 17:53 Comment(2)
I assume this bug is already fixed. using jekyll 3.2.1 and ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14] it's workingRebirth
Now it works fine.Consentaneous
T
138

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' }}
Thereupon answered 17/9, 2012 at 20:5 Comment(7)
or {{ site.time | date: '%Y' }} for 2014Elaineelam
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.Inhumation
@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.Thereupon
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)Inhumation
@menehune23 Not sure if you're implying that this workaround is any different than what the OP was trying to do.Thereupon
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 latterInhumation
Lowercase %y would print two digit year like 22 whereas uppercase %Y would print full four digit 2022Vivle
M
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' }}
Martinemartineau answered 3/12, 2015 at 15:50 Comment(3)
This basically repeats the comment after my answer.Thereupon
@MarkThomas But still have the great explanation.Martinemartineau
@MarkThomas You should update your answer with the post-Ruby 1.9.3 answer.Zitella
W
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

Willamina answered 4/1, 2019 at 14:43 Comment(0)
P
-1

{{ 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>
Phonetician answered 20/7, 2020 at 12:21 Comment(1)
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(); // 2021Neonatal

© 2022 - 2024 — McMap. All rights reserved.