4

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!

jvc26
  • 6,363
  • 6
  • 46
  • 75
  • nice question, lots of people use Tom's template now meet this problem https://github.com/mojombo/tpw/blob/master/_layouts/post.html – Larry Cai Jan 19 '13 at 14:15

1 Answers1

8

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

Community
  • 1
  • 1
joshuahornby10
  • 4,222
  • 8
  • 36
  • 52
  • As per the second answer to your linked, date_to_string is part of the standard filters that Jekyll adds to liquid. It works on GH Pages, which probably means its related to a Ruby/Jekyll/Liquid version mixup, just wondered whether anyone had pointers regarding that? – jvc26 Jan 04 '13 at 17:24
  • 1
    Why do you need to convert it to a string? why not just generate it using the way I have done on my site? – joshuahornby10 Jan 05 '13 at 00:09
  • Essentially, it does exactly the same as ```| date "%d %b %Y"```, I was just interested by the fact that it doesn't appear to work given some combinations of Jekyll and Ruby, which seems odd! – jvc26 Jan 05 '13 at 10:33