Web Island Agency

How to format dates in Jekyll

How to format dates in Jekyll
How to format dates in Jekyll

There are various ways how to write a date all around the world. This differs from country to country, and sometimes it can be confusing. For example, you cannot say for sure what 10/10/12 means. It could be interpreted as either 10 October 2012 or as 10 November 2012 depends on the writer’s country. This article describes how to control date formatting in Jekyll.

Jekyll provides built-in filters as well as features to build your own. You can see the list of built-in filters below.

date_to_long_string

The filter converts date to the common format with a full month name. Use and output example:

{{ page.date | date_to_long_string }}
Output example: 22 June 2019

date_to_string

The filter converts date to the common format with a short month name. Use and output example:

{{ page.date | date_to_string }}
Output example: 22 Jun 2019

date_to_rfc822

The filter converts date to the RFC format. Use and output example:

{{ page.date | date_to_rfc822 }}
Output example: Thu, 22 Jun 2019 21:00:00 +0000

date_to_xmlschema

The filter formats date for use in XML. Use and output example:

{{ page.date | date_to_xmlschema }}
Output example: 2019-06-22T21:00:00+00:00

date

Usually, you need more specific formats of date and Liquid gives you a hand with this. The special filter date converts a timestamp into a desired date format.

Some examples and their outputs:

{{ page.date | date: "%b %d, %Y" }}
Output example: Jun 22, 2019

{{ page.date | date: "%j" }}
Output example: 173

{{ page.date | date: "%A, %B %d, %y" }}
Output example: Saturday, June 22, 2019

{{ page.date | date: "%D" }}
Output example: 22/06/19

{{ page.date | date: "%Z" }}
Output example: EGST

{{ page.date | date: "%r" }}
Output exmaple: 11:43:20 PM

{{ page.date | date: "%e %B, %Y %H:%M %Z" }}
Output example: 22 June, 2019 14:42 EEST

date filter formatting string syntax

The filter formatting string syntax is identical with the strftime table:

specifier replaced by example
%A full weekday name Monday, Friday
%b abbreviated month name April, May, June
%B full month name Apr, May, Jun
%c appropriate date and time representation Thu Jan 28 12:32:01 2014
%d day of the month (with a leading zero) 01, 05, 24
%D formats the date 23/05/12
%e day of the month (without a leading zero) 02, 04, 12
%F date in ISO 8601 format YYYY-MM-DD 2002-01-30
%H hour of the day, 24-hour time 01-23
%I hour of the day, 12-hour time 01-11
%j day of the year 084
%k hour of the day, 24-hour time (without a leading zero) 0-23
%m month of the year 01-12
%M minute of the hour 00-59
%p AM or PM designation AM, PM
%P am or pm designation am, pm
%r 12-hour time 3:44:12 AM
%R 24-hour time HH:MM 15:21
%T time in ISO 8601 format HH:MM:SS 15:21:59
%s amount of seconds since 1970-01-01 00:00:00 UTC
%S second of the minute 00-60
%U week number with the first Sunday as the first day of week one 00-53
%V week number in ISO 8601 format 01-53
%w day of the week with Sunday as 0 0-6
%W week number of the current year, starting with the first Monday as the first day of the first week 00-53
%x preferred date representation 02/10/11
%X preferred time representation 14:22:01
%y year without a century (last two digits) 00-99
%Y year 1939, 2003
%Z timezone name or abbreviation EEST, CDT
%% a % sign

Conclusion

As you can see, Jekyll provides build-in filters to format dates: date_to_long_string, date_to_string, date_to_rfc822, date_to_xmlschema, and date for custom data formatting.

Join the discussion!

The article is published under the tags

Read more articles related to the topic