Date and time
Date
Outputs a specific date format of a string.
{{ "31/12/2018" | date:"%Y" }}
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)
%-d - Day of the month (1..31)
%F - Returns the date in ISO 8601 format (2021-01-29)
%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" }}
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' }}
New Hour: 11:10
Comparing with dates
Not all dates are formatted in the same way, as it depends on the kind of variable it is. We can distinguish the following kind of variables and their date format by default:
Input variable with the date attribute
{% input custom.report.date_gm as:date placeholder:"DD/MM/YYYY" %}
will be formatted as dd/mm/yyyy (eg. 14/02/2022).
Local variables
Local variables can be formatted with any date format:
{% assign my_date = "2022-12-31" | date:"%Y" %}
will give 2022 as output.
Period drop dates
The period drop dates are formatted as ‘yyyy-mm-dd’ by default, such as:
{{ period.year_end_date }}
will give 2022-12-31 as output.
Due to different date formats possible, it is advised to always convert any date to the following specific date filter:
date:"%F"
which converts any date to the format yyyy-mm-dd.
Even if 2 dates of the same kind of variable are compared (eg. inputs with the date attribute), it is a good practise to convert them anyway to this specific date format with the filter
%F
.
Click here for a related Community case.
Updated 9 months ago