Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to print post date in Jekyll fails with undefined method `strftime'

Tags:

ruby

jekyll

I've been using Liquid extensions to reformat dates on my Jekyll based site, for example:

<p>{{ post.date | date_to_string }}</p>

This works fine in my index.html page which just takes the five most recent posts and then iterates them post by post. However, this fails when I am trying to render such a date within my _layouts/base.html template.

I have tried:

{{ page.date | date_to_string }}

{{ page.title }} works without issue, and {{ page.date}} renders when I use it without the liquid filter, outputting, for example, 2012-03-12 00:00:00 +0000.

Why does the date_to_string filter fail with the input provided by {{ page.date }}. I get the following error:

Liquid Exception: undefined method `strftime' for nil:NilClass in base

And the build fails. Thanks for any help!

like image 453
jvc26 Avatar asked Feb 17 '23 23:02

jvc26


1 Answers

On my site I use

{{ page.date | date: "%d %B %Y" }}

It grabs the date from the markdown file. And is rendered like so:

26 December 2012

See these links for some extra reading:

How does Jekyll date formatting work?

http://liquid.rubyforge.org/classes/Liquid/StandardFilters.html#M000012

EDIT: To answer your question in the comments section:

If you want to use date_to_string you have to call it like this:

{{ site.time | date_to_string }}

There is also {{ site.time | date_to_long_string } which will write the month out in it's full form eg. November not Nov.

Source

https://github.com/mojombo/jekyll/wiki/Liquid-Extensions

like image 117
joshuahornby10 Avatar answered Apr 08 '23 05:04

joshuahornby10