Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom date formats #164

Closed
sfahmy opened this issue Sep 21, 2011 · 22 comments
Closed

Allow custom date formats #164

sfahmy opened this issue Sep 21, 2011 · 22 comments
Milestone

Comments

@sfahmy
Copy link

sfahmy commented Sep 21, 2011

It seems the date formats for display are coded deep in the engine somewhere and not exposed through the templates or configuration files. It would be good to allow users to modify the date format.

@fhemberger
Copy link
Contributor

Date formats are exposed to the templates, use the Liquid's date filter, e.g.

{{ post.date | date: "%Y-%m-%d" }}

List of format descriptors: http://liquid.rubyforge.org/classes/Liquid/StandardFilters.html#M000012

@sfahmy
Copy link
Author

sfahmy commented Oct 18, 2011

So is there a chance of getting date format added as a top-level preference? Otherwise, where in the template files must this be modified?

@sfahmy
Copy link
Author

sfahmy commented Oct 18, 2011

Looking through the source, the files all use _includes/post/date.html, but I see no such line in there? I think this being an option in the config file is useful, e.g. date_format: %d %M %Y in _config.yml, then all the template files can be updates so they use this. Otherwise, one must hack the template source, which isn't ideal.

@fhemberger fhemberger reopened this Oct 18, 2011
@fhemberger
Copy link
Contributor

Good point. I'll add a config flag for date …

@fhemberger
Copy link
Contributor

I had a look at this issue again, unfortunately something like {{ date | date: "{{ site.date_format }}" }} isn't evaluated by Liquid and a filter (like ordinalize) doesn't know he's running in a Jekyll context, so I can't access the config variables at this point without ugly hacking.

@sfahmy
Copy link
Author

sfahmy commented Oct 18, 2011

Interesting. Which template files would need to be edited, and how? It could be added to the documentation, I suppose.

Why not let Jekyll/Ruby take care of the date format? It could be argued that the date format is something a site author would want to set regardless of the template used, so not using the Liquid filter isn't so bad.

@fhemberger
Copy link
Contributor

You only need to modify _includes/post/date.html to adjust the date output.

Default date output in Ruby looks like this: 2011-10-18 12:30:46 +0200 ... not very pretty. Jekyll doesn't provide methods to format the output, it's only provided by Liquid (the template engine used). And that's the filter described above.

@sfahmy
Copy link
Author

sfahmy commented Oct 18, 2011

I was thinking more of Ruby's strftime method:
http://www.ruby-doc.org/core-1.9.2/Time.html#method-i-strftime

This could then be used in conjunction with a config variable.

@fhemberger
Copy link
Contributor

Ok, I found a solution: I monkey patched Jekyll's to_liquid method to not just provide date but also date_formatted, which returns a strftime() formatted version of the date, which can be configured in _config.yml. I hope this will solve your issue, will commit it later.

@sfahmy
Copy link
Author

sfahmy commented Oct 18, 2011

Brilliant! Should make those of us not in the US happier with the date formats!

@sfahmy
Copy link
Author

sfahmy commented Nov 2, 2011

Turns out this works for blog posts, but not static pages. The time information in the tags is empty. Is it supposed to take care of formatting the updated date too?

@fhemberger
Copy link
Contributor

It's only applied to posts so far, but I'll add it for pages, too (I need to override Jekyll's own methods for that).

In a later commit, I removed the support for "updated":

Jekyll does not support this value for Page or Post classes. The
capture returned an empty Sting, which was always evaluated to true
in '{% if updated %}'.

@sfahmy
Copy link
Author

sfahmy commented Nov 3, 2011

Does that mean that other commit mean we can no longer get last updated info displayed on pages? It was working previously.

@fhemberger
Copy link
Contributor

@sfahmy Do you set the updated date manually in the YAML header of your post?
I only removed the flag, because it's not provided by Jekyll (e.g. in case you update your post). So I was wondering, where it came from ...

@sfahmy
Copy link
Author

sfahmy commented Nov 3, 2011

Correct, I set the updated to a date in the YAML header. That would result in the updated date being added at the bottom of pages. It's more useful for static pages than blog posts.

@sfahmy
Copy link
Author

sfahmy commented Nov 3, 2011

It looks like this is already somehow doable in Jekyll:
http://stackoverflow.com/questions/7395520/jekyll-date-format

@fhemberger
Copy link
Contributor

Ah, ok. I'll add back support for updated and will commit the changes in the evening. I'm sorry, if this caused any inconvenience for you.

fhemberger added a commit that referenced this issue Nov 3, 2011
Reverts changes of c2a68cc where I accidentally removed support for 'updated' field, see comments of issue #164 for details.
@fhemberger
Copy link
Contributor

@sfahmy: Updated flag is back and custom dates work on pages, too.

@sfahmy
Copy link
Author

sfahmy commented Nov 4, 2011

Great, works perfectly!

Now I need to see how I might hack it so it ordinalizes even non standard date formats :) But that looks hard!

@fhemberger
Copy link
Contributor

No, it's not. ;) I just added %o as ordinal representation of the day. Just use it in your date format string.

@sfahmy
Copy link
Author

sfahmy commented Nov 4, 2011

Fantastic! There's an error on line 39 though. It should read :
format.gsub!(/%o/, '%o')

The extra % messed things up, such that each page would have an increasing number of % signs prepending the date.

I just fixed it locally.

@fhemberger
Copy link
Contributor

The second % was used as an escapement, but obviously this is not needed in Ruby. You can remove the line altogether.

briansimmons pushed a commit to briansimmons/octopress that referenced this issue Aug 20, 2013


A new config variable 'date_format' is introduced in _config.yml.
It can either be set to "ordinal" to use the current format or it
can be given a string complying to strftime() format identifiers.
briansimmons pushed a commit to briansimmons/octopress that referenced this issue Aug 20, 2013
Reverts changes of c2a68cc where I accidentally removed support for 'updated' field, see comments of issue imathis#164 for details.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants