DevHub Docs
Search…
Using date and time in templates
Formatting and comparison on dates and times

Template utilities

datefmt

Format a date/time to output a specific format.
Example
Output
{{ timestamp|datefmt('long') }}
January 1, 2021
{{ timestamp|datefmt('Y') }}
2021
More examples of the formats available can be found here: http://babel.pocoo.org/en/latest/dates.html#date-fields

now

This is quick access to the current date and time
1
<p>The current date is: {{ now|datefmt('MMMM d') }}</p>
Copied!
Output
1
<p>The current date is: January 1</p>
Copied!

day_name

Output the name of the day using the "0 index" of the day of the week.
1
{{ 3|day_name }}
Copied!
Output
1
Wednesday
Copied!

to_date

Utility to parse a date from a string
1
{{ '2021-09-01'|to_date }}
Copied!
Optionally, you can also pass a format to the parsing
1
{{ '9/1/2021'|to_date(format='%m/%d/%y') }}
Copied!

from_timestamp

Can be used to convert unix timestamps (i.e. an integer) into date/time to be used by other functions.
1
{{ 1284101485|from_timestamp|datefmt('long') }}
Copied!
Output
1
September 9, 2010
Copied!

Hide content based on date

Using the advanced jinja2 templating to hide a piece of content depending on the date. This can be used to have something show up before/after/during a date range.
This uses our to_date jinja filter. Take note of the greater than and less than operators.
The to_date is parsed as the first minute and hour of the day so using greater and less than operators is the best way to write the conditionals.

Today, future, or past comparisons

is_today

Show something if the date is today
1
{% if '2018-12-11'|to_date|is_today %}
2
<p>I will show if Dec 12, 2018 is today</p>
3
{% endif %}
Copied!

is_future

Show something if the date is in the future
1
{% if '2018-12-11'|to_date|is_future %}
2
<p>I will show if Dec 12, 2018 is in the future</p>
3
{% endif %}
Copied!

is_past

Show something if the date is in the past
1
{% if '2018-12-11'|to_date|is_past %}
2
<p>I will show if Dec 12, 2018 is in the past</p>
3
{% endif %}
Copied!

Combining the above filters

Show something if the date is today or in the future
1
{% if '2018-12-11'|to_date|is_today or '2018-12-11'|to_date|is_future %}
2
<p>I will show if Dec 12, 2018 is today or is in the future</p>
3
{% endif %}
Copied!

Using now to compare to the current day and time

The now is the current date and time

Starting on a date

1
{% if now > '2018-12-11'|to_date %}
2
<p>I will start showing on Dec 11, 2018</p>
3
{% endif %}
Copied!

Up to a date

1
{% if now < '2018-12-11'|to_date %}
2
<p>I will stop showing on Dec 11, 2018</p>
3
{% endif %}
Copied!

Date range

1
{% if now > '2018-12-01'|to_date and now < '2019-01-01'|to_date %}
2
<p>I will show for the whole month of December</p>
3
{% endif %}
Copied!
Last modified 2mo ago