πŸ•’ 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

ParameterDescriptionExample
ssSeconds (00-59)45
mmMinutes (00-59)23
HHHours (00-23)13
ddDay (two digits)07
dDay (one digit)7
DDDay of the year238
MMMonth (two digits)08
MMonth (one digit)8
yyyyYear (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" returns the current datetime in the Europe/Paris timezone.

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

  • 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
      

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

{{ "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.