VarunDevBlog

Date formatting in jekyll

Introduction

Formatting dates can be tricky sometimes when you need a specific format. Here I show you how to get your work done with dates using some filters and placeholders.

Setup

We’ll start by setting a date in front matter in ISO 8601 format.

---
layout: default
title: Some title of a page
date: 2016-03-23T10:20:00Z
---

Use filters

Now we can run the date through a filter to get the desired format.

Input:

{{ page.date | date_to_long_string }}

Output:

23 March 2016

Similar filters

Filter Example
date_to_long_string 23 March 2016
date_to_rfc822 Wed, 23 Mar 2016 23:20:00 +1300
date_to_string 23 Mar 2016
date_to_xmlschema 2016-03-23T23:20:00+13:00

Use placeholders

date gives us complete control of the format. We can specify a template of the format we want. For example.

Input:

{{ page.date | date: "%-d %B %Y"}}

Output:

03/23/2016

There’s many placeholders we can use for date formatting.

Placeholder Format Example
%a Abbreviated weekday Sun
%A Full weekday name Sunday
%b Abbreviated month name Jan
%B Full month name January
%c Preferred local date and time representation Fri Jan 29 11:16:09 2016
%d Day of the month, zero-padded 05
%-d Day of the month 5
%D Formats the date 29/01/16
%e Day of the month 3
%F Returns the date in ISO 8601 format 2016-01-29
%H Hour of the day, 24-hour clock 07
%I Hour of the day, 12-hour clock 04
%j Day of the year 017
%k Hour of the day, 24-hour clock 7
%m Month of the year 04
%M Minute of the hour 09
%p Meridian indicator uppercase AM
%P Meridian indicator lowercase pm
%r 12-hour time 01:31:43 PM
%R 24-hour time 18:09
%T 24-hour time with seconds 18:09:13
%s Number of seconds since 1970-01-01 00:00:00 UTC 1452355261
%S Second of the minute 05
%U Week number of the current year, starting with the first Sunday as the first day of the first week 03
%W Week number of the current year, starting with the first Monday as the first day of the first week 09
%w Day of the week. Sunday is 0 4
%x Preferred representation for the date 05/11/15
%X Preferred representation for the time 17:15:31
%y Year without a century 16
%Y Year with a century 2016
%Z Time zone name PST
%% Literal % character %

More Info