🕒 Date, Time, and Duration Management in Jinja
This guide covers how to handle dates and times in Jinja, focusing on formatting, timedelta calculations, and best practices to avoid common pitfalls.
⚙️ Basic Jinja Parameters for Date and Time
| Parameter | Description | Example |
|---|---|---|
ss | Seconds (00-59) | 45 |
mm | Minutes (00-59) | 23 |
HH | Hours (00-23) | 13 |
dd | Day (two digits) | 07 |
d | Day (one digit) | 7 |
DD | Day of the year | 238 |
MM | Month (two digits) | 08 |
M | Month (one digit) | 8 |
yyyy | Year (four digits) | 2023 |
Always use
"yyyy"for years to avoid issues with year-end calculations.
1. Datetime Handling
now - Getting the Current Date and Time
now - Getting the Current Date and Time"now"returns the current datetime in theEurope/Paristimezone.
to_date - Converting a String to a Date Object
to_date - Converting a String to a Date Object-
Usage: Standardizes date formats, making them ready for comparison and calculation.
{{ "20181024"| to_date }} > 2018-10-24 00:00:00+01:00
datetime - Formatting Date Objects as Strings
datetime - Formatting Date Objects as Strings-
Usage: Extract specific date elements and format for user readability.
{{ "20181024" |to_date |datetime('dd') }} > 24 {{ "20181024" |to_date |datetime("yyyy-MM-dd") }} > 2018-10-24- Display a nicely formatted date:
{{ "20190719" |to_date|datetime('EEEE dd MMMM yyyy', 'CET', 'fr_FR') }} > vendredi 19 juillet 2019
- Display a nicely formatted date:
2. Timedelta Calculations
When you subtract two date objects, you get a timedelta, which represents the difference.
{{ ("20181024" |to_date) - ("20181023" |to_date) }}
> 1 day, 0:00:00
to_timedelta - Casting Timedeltas from Strings
to_timedelta - Casting Timedeltas from Strings{{ "1 day, 0:10:00" |to_timedelta }}
> 1 day, 0:10:00
Formatting Timedeltas
-
Default formatting:
{{ "1 day, 0:10:00" |to_timedelta |timedelta }} > 24 h -
Threshold usage:
{{ "0 day, 0:09:00" |to_timedelta |timedelta('second', 10) }} > 540 s
3. Timestamp Creation
{{ ("now"|datetime("yyyy-MM-dd'T'HH:mm:ssZZZZZ")|to_date).timestamp()|int }}
> 1626789134
📅 Best Practices for Formatting: Use ISO 8601
Using ISO formats (e.g., yyyy-MM-dd) prevents ambiguity, especially across different locales.
Example of Good Practice
{{ "now"|datetime("yyyy-MM-dd") }}
> 2021-03-11
{{ "now"|datetime("yyyy-MM-dd")|to_date }}
> 2021-03-11 00:00:00+01:00
🔍 Examples of Common Date and Time Operations
Adding Days to Today's Date
{{ "now"|datetime('yyyy-MM-dd')|to_date + "5 days, 00:00:00"|to_timedelta }}
Check if Enough Time Has Passed
{% if "now"|datetime('yyyy-MM-dd')|to_date > user_date|to_date + "5 days, 00:00:00"|to_timedelta %}
true
{% endif %}
Formatting with AM/PM
{{ "2019-01-09 13:01:35"|to_date |datetime("EEEE dd MMMM yyyy hh'h'mm a", "CET", 'en_GB') }}
> Wednesday 09 January 2019 01h01 pm
Directional and Granular Timedelta Formatting
Loyalty points credit date: {{ (("2018-12-24 11:35:00"|to_date + "120 days, 00:00:00"|to_timedelta) - "now"|datetime('yyyy-MM-dd')|to_date) |timedelta('day', 10, True, 'long', 'en_GB') }}
> in 12 weeks
Parsing European Date Formats (dd/MM/yyyy)
By default, to_date expects MM/dd/yyyy. Adjust this to avoid parsing errors, and consider standardizing with ISO formats where possible.
