Skip to content

Instantly share code, notes, and snippets.

@robweychert
Last active February 18, 2020 14:35
Show Gist options
  • Save robweychert/7d7a484c6a85085269b455eef2c171c4 to your computer and use it in GitHub Desktop.
Save robweychert/7d7a484c6a85085269b455eef2c171c4 to your computer and use it in GitHub Desktop.
AP style month abbreviations with strftime in Liquid

AP style month abbreviations with strftime in Liquid

AP style is particular about how dates are formatted in various circumstances. strftime uses %b for month abbreviations, but its format (the first three letters of the month: Jan, Feb, etc) differs from AP style’s preferred abbreviations for some months. This Liquid snippet converts strftime’s month abbreviations to AP style:

{{ object | date: '%b. %e, %Y' | replace: 'Mar.', 'March' | replace: 'Apr.', 'April' | replace: 'May.', 'May' | replace: 'Jun.', 'June' | replace: 'Jul.', 'July' | replace: 'Sep.', 'Sept.' }}

For example, this Liquid code…

{% assign today = '2020-09-15' %}
<p class="current-date">{{ today | date: '%b. %e, %Y' | replace: 'Mar.', 'March' | replace: 'Apr.', 'April' | replace: 'May.', 'May' | replace: 'Jun.', 'June' | replace: 'Jul.', 'July' | replace: 'Sep.', 'Sept.' }}</p>

…will generate this HTML:

<p class="current-date">Sept. 15, 2020</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment