Home » Python » Python: How do I format a date in Jinja2?

Python: How do I format a date in Jinja2?

Posted by: admin November 1, 2017 Leave a comment


Using Jinja2, how do I format a date field? I know in Python I can simply do this:

print car.date_of_manufacture.strftime('%Y-%m-%d')

But how do I format the date in Jinja2? Thanks.


There are two ways to do it. The direct approach would be to simply call (and print) the strftime() method in your template, for example

{{ car.date_of_manufacture.strftime('%Y-%m-%d') }}

Another, sightly better approach would be to define your own filter, e.g.:

from flask import Flask
import babel

app = Flask(__name__)

def format_datetime(value, format='medium'):
    if format == 'full':
        format="EEEE, d. MMMM y 'at' HH:mm"
    elif format == 'medium':
        format="EE dd.MM.y HH:mm"
    return babel.dates.format_datetime(value, format)

app.jinja_env.filters['datetime'] = format_datetime

(This filter is based on babel for reasons regarding i18n, but you can use strftime too). The advantage of the filter is, that you can write

{{ car.date_of_manufacture|datetime }}
{{ car.date_of_manufacture|datetime('full') }}

which looks nicer and is more maintainable. Another common filter is also the “timedelta” filter, which evaluates to something like “written 8 minutes ago”. You can use babel.dates.format_timedelta for that, and register it as filter similar to the datetime example given here.


Here’s the filter that I ended up using for strftime in Jinja2 and Flask

def _jinja2_filter_datetime(date, fmt=None):
    date = dateutil.parser.parse(date)
    native = date.replace(tzinfo=None)
    format='%b %d, %Y'
    return native.strftime(format) 

And then you use the filter like so:



I think you have to write your own filter for that. It’s actually the example for custom filters in the documentation: http://jinja.pocoo.org/docs/api/#custom-filters


If you are dealing with a lower level time object (I often just use integers), and don’t want to write a custom filter for whatever reason, an approach I use is to pass the strftime function into the template as a variable, where it can be called where you need it.

For example:

import time
    'strftime':time.strftime }  # Note there are no brackets () after strftime
                                # This means we are passing in a function, 
                                # not the result of a function.

self.response.write(jinja2.render_template('sometemplate.html', **context))

Which can then be used within sometemplate.html:

        <p>The time is {{ strftime('%H:%M%:%S',now) }}, and 5 seconds ago it was {{ strftime('%H:%M%:%S',now-5) }}.


Google App Engine users : If you’re moving from Django to Jinja2, and looking to replace the date filter, note that the % formatting codes are different.

The strftime % codes are here: http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior


in flask, with babel, I like to do this :

def _jinja2_filter_datetime(date, fmt=None):
    if fmt:
        return date.strftime(fmt)
        return date.strftime(gettext('%%m/%%d/%%Y'))

used in the template with {{mydatetimeobject|dt}}

so no with babel you can specify your various format in messages.po like this for instance :

#: app/views.py:36
#, python-format
msgid "%%m/%%d/%%Y"
msgstr "%%d/%%m/%%Y"