Sling Academy
Home/FastAPI/How to format date time in Jinja templates

How to format date time in Jinja templates

Last updated: November 28, 2024

Jinja is a popular templating engine for Python, commonly used in frameworks like Flask and Django. When building web applications, you often need to display dates and times, but formatting these in Jinja templates can be tricky without some guidance. This article will explore various ways to format date and time using Jinja templates.

Basic Date and Time Formatting in Jinja

To get started with formatting, let's assume you have a datetime object that you need to format in your Jinja template.

{{ my_date_variable.strftime("%Y-%m-%d") }}

In this example, {{ my_date_variable.strftime("%Y-%m-%d") }} is used to convert a date object into a string in the YYYY-MM-DD format. Here's a breakdown of the format codes:

  • %Y: Year with century (e.g., 2023)
  • %m: Month as a zero-padded decimal number (e.g., 01 for January)
  • %d: Day of the month as a zero-padded decimal number (e.g., 07)

Using Filters for Date Formatting

Jinja also provides a convenient filter called strftime for formatting. It's particularly handy when you pass a datetime object to the template directly.

{{ my_date_variable|strftime("%B %d, %Y") }}

In this example, the strftime filter formats my_date_variable to a more reader-friendly format (e.g., "October 07, 2023"). Here's an explanation of the format codes used:

  • %B: Full month name (e.g., "October")
  • %d: Day of the month as a zero-padded decimal number
  • %Y: Year with century

Displaying Time

To display time, you can extend the formats to include hours, minutes, and seconds. Here's an example:

{{ my_time_variable|strftime("%H:%M:%S") }}

This format will output the time portion in the 24-hour HH:MM:SS format.

  • %H: Hour (24-hour clock) as a zero-padded decimal number
  • %M: Minute as a zero-padded decimal number
  • %S: Second as a zero-padded decimal number

Combining Date and Time

Combining date and time in a format is straightforward. Here's a combined example:

{{ my_datetime_variable|strftime("%Y-%m-%d %H:%M:%S") }}

This outputs the full datetime in the format YYYY-MM-DD HH:MM:SS.

Conclusion

Jinja's formatting capabilities, while not immediately obvious, are robust and flexible. By leveraging filters such as strftime and understanding Python's extensive formatting options, you can easily customize how dates and times appear in your Jinja-rendered web applications. Experiment with different format strings to better meet your application's needs and to present information in the most user-friendly way possible.

Next Article: How to format large numbers with thousand separators in Jinja template

Previous Article: FastAPI + Jinja: How to create custom filters

Series: Jinja Templates

FastAPI

You May Also Like

  • Popular useful built-in Jinja filters you should know
  • How to remove consecutive whitespace in rendered Jinja pages
  • How to format large numbers with thousand separators in Jinja template
  • FastAPI + Jinja: How to create custom filters
  • How to pass variables from Python (FastAPI) to Jinja
  • How to decode Jinja response to string
  • How to create and use macros in Jinja
  • How to use namespace in Jinja
  • How to use if/ else in Jinja
  • How to use loops in Jinja
  • FastAPI + SQLAlchemy: Using cursor-based pagination
  • FastAPI: How to use macros in Jinja templates
  • Fixing Common Swagger UI Errors in FastAPI
  • FastAPI Error: 307 Temporary Redirect – Causes and Solutions
  • FastAPI Error: Expected UploadFile, received ‘str’
  • Resolving FastAPI ImportError: No Known Parent Package
  • FastAPI Error: No module named ‘pydantic_core._pydantic_core’
  • Resolving FastAPI 422 Error: Value is not a valid dict
  • Resolving the FastAPI Circular References Error