Skip to content

Instantly share code, notes, and snippets.

@WheresWardy
Created June 4, 2013 11:19
Show Gist options
  • Save WheresWardy/5705223 to your computer and use it in GitHub Desktop.
Save WheresWardy/5705223 to your computer and use it in GitHub Desktop.
Jekyll/Liquid plugin to add an ordinal suffix to dates
module Jekyll
module DateFilter
def ord(v)
(['1', '21', '31'].include?(v) && "st") ||
(['2', '22'].include?(v) && "nd") ||
(['3', '23'].include?(v) && "rd") ||
"th"
end
def date_post(postdate)
ord = ord(postdate.strftime("%-d"))
postdate.strftime("%-d#{ord} %B %Y")
end
end
end
Liquid::Template.register_filter(Jekyll::DateFilter)
@WheresWardy
Copy link
Author

You can then adjust strftime to give you a date of your liking, chucking in #{ord} wherever you need it, and include the date like so:

{{ post.date | date_post }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment