Search code examples
datejekyllliquid

How can I get ordinal dates in Jekyll?


In Jekyll, I would really like to have the date format as "2nd December 2012" — i.e. with the ordinal, rather than just "2 December 2012".

I see that there's an ordinalize function available in Ruby to do this, but I haven't had any luck combining it with Liquid and Jekyll.

How can I format dates as I would like?


Solution

  • I solved this by writing the following Jekyll plugin, which I placed in the _plugins directory with an arbitrary name, e.g. date.rb:

    require 'date'
    require 'facets/integer/ordinal'
    
    module Jekyll
      module DateFilter
        def pretty(date)
          "#{date.strftime('%e').to_i.ordinalize} #{date.strftime('%B')} #{date.strftime('%Y')}"
        end  
      end
    end
    
    Liquid::Template.register_filter(Jekyll::DateFilter)
    

    Then — having installed the facets Ruby gem — I was able to use {{ post.date | pretty }} in my site, and all was well.