Date and time

Date

Outputs a specific date format of a string.

{{ "31/12/2018" | date:"%Y" }}
Output
2018

📘

Usable formats

%a - The abbreviated weekday name (Sun)
%A - The full weekday name (Sunday)
%b - The abbreviated month name (Jan)
%B - The full month name (January)
%c - The preferred local date and time representation
%d - Day of the month (01..31)
%H - Hour of the day,24-hour clock (00..23)
%I - Hour of the day,12-hour clock (01..12)
%j - Day of the year (001..366)
%m - Month of the year (01..12)
%M - Minute of the hour (00..59)
%p - Meridian indicator (AM or PM)
%S - Second of the minute (00..60)
%U - Week number of the current year, starting with the first Sunday as the first day of the first week (00..53)
%W - Week number of the current year, starting with the first Monday as the first day of the first week (00..53)
%w - Day of the week (Sunday is 0,0..6)
%x - Preferred representation for the date alone,no time
%X - Preferred representation for the time alone,no date
%y - Year without a century (00..99)
%Y - Year with century
%Z - Time zone name
%% - Literal % character

There are several filters that can be used to interact with times and dates. This is a list of them:

Advance filter

Retract filter

advance_years

retract_years

advance_months

retract_months

advance_weeks

retract_weeks

advance_days

retract_days

advance_hours

retract_hours

advance_minutes

retract_minutes

advance_seconds

retract_seconds

For example:

{% assign prev_date = '2019-06-20' %}
{% assign new_date = prev_date | advance_months:3 | advance_years:1  %}

Original Date: {{ prev_date | date:"%d/%m/%Y" }}
New Date: {{ new_date | date:"%d/%m/%Y" }}
Output
Original Date: 20/06/2019
New Date: 20/09/2020

Another example:

{% assign hour = '10/06/2019 15:45:00' %}
{% assign new_hour = hour | retract_hours:5 | advance_minutes:25 %}

Original Hour: {{ hour | date:'%H:%M' }}
New Hour: {{ new_hour | date:'%H:%M' }}
Output
Original Hour: 15:45
New Hour: 11:10

Comparing with dates

Dates created using input tags
You can directly compare these dates without converting them.

📘

Converting dates to dd/mm/yyyy (%d/%m/%Y)

Although there is no need to do a format conversion when comparing input dates (i.e. input date vs input date). When calculating with dates, the arithmetic formulas will only work with dates formatted as dd/mm/yyyy (e.g. calculating the number of days between two days).

{% input custom.my.date_1 as:date %}
{% input custom.my.date_2 as:date %}

{% if custom.my.date_1 > custom.my.date_2 %}
  Date 1 is greater than Date 2
{% endif %}
Output

Date 1 is greater than Date 2
Period drop dates
Period drop dates such as period.year_end_date are saved in the database with this format: "yyyy-mm-dd" so in order to compare them we need to convert other dates to this format and make them strings using capture.
{% input custom.my.date_1 as:date %}

{{ period.year_end_date }}

{% capture year_end_date %}{{ period.year_end_date }}{% endcapture %}
{% capture date_1 %}{{ custom.my.date_1 | date:'%Y-%m-%d' }}{% endcapture %}

{% if date_1 == year_end_date %}
  Date 1 is equal to Year End Date
{% endif %}
Output

Date 1 is equal to Year End Date
Local dates
We can create our own dates with assign using the "yyyy-mm-dd" format.
{% assign date_1 = "2020-12-31" %}
{% assign date_2 = "2015-12-31" %}

{% if date_1 > date_2 %}
  Date 1 is greater than Date 2
{% endif %}
Output
Date 1 is greater than Date 2