Adds `parse_date` and `format_date` filters to Jinja.
Project description
Jinja2-ISO8601
Adds parse_date
and format_date
filters to Jinja.
The iso8601
module handles date parsing,
hence the name of this project. The
.strftime()
method handles date formatting.
Install
First install the Python module:
pip install jinja2-iso8601
Then add the extension into your Jinja environment:
from jinja2 import Environment
jinja_env = Environment(extensions=['jinja2_iso8601.ISO8601Extension'])
Use
You can now use two new filters when writing your Jinja templates:
parse_date
and format_date
.
parse_date
will turn an ISO-8601 formatted string into a
Python datetime
object.
format_date
will call the .strftime()
method on whatever you pass to it;
this works well with datetime
, date
, and time
objects. You'll need to
provide a format string for the format_date
filter;
check the Python documentation for how to write a format string.
For example, let's say you have a variable called datestr
which is
a string representing a datetime in valid ISO-8601 format, such as
"2022-09-19T14:38:34.213001"
. You can write your Jinja template like this:
no filter: {{ datestr }}
parsed: {{ datestr|parse_date }}
formatted: {{ datestr|parse_date|format_date("%a, %b %d %Y") }}
and the rendered result will be:
no filter: 2022-09-19T14:38:34.213001
parsed: 2022-09-19 14:38:34.213001
formatted: Mon, Sep 19 2022
Timezones
In Python, date and time objects may include timezone information. Date and time objects that have an associated timezone are "aware", and those that do not have an associated timezone are "naive".
By default, when parsing ISO-8601 strings without any timezone information,
the result is a "naive" datetime object. However, in some cases, you may
want to assume a default timezone, so that you always get an "aware"
datetime object even if no timezone is specified. For example, if you know
that the ISO-8601 string is in the UTC timezone, you may want the
parse_date
filter to return an "aware" datetime with the UTC timezone.
The default_timezone
value on the Jinja environment controls which
timezone to use when none is specified in the string. By default, this
value is None
, which makes parse_date
return a "naive" datetime.
Here's how to make all datetimes parse as UTC:
from datetime import timezone
from jinja2 import Environment
jinja_env = Environment(extensions=['jinja2_iso8601.ISO8601Extension'])
jinja_env.default_timezone = timezone.utc
See Also
These filters can be paired with other Jinja filters that process datetimes,
such as jinja2_humanize_extension
.
You can parse strings into datetime using the parse_date
filter from this
project, and then "humanize" the datetime using the humanize_naturalday
filter
from jinja2_humanize_extension
. They work together seamlessly!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file jinja2_iso8601-1.0.0.tar.gz
.
File metadata
- Download URL: jinja2_iso8601-1.0.0.tar.gz
- Upload date:
- Size: 3.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.27.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e1913997cae4576425b5d0399743008b85ae10fc4d9a1a8833648fba9f56fbc |
|
MD5 | 64b95decb10bd6ca109f1fbcb1819e77 |
|
BLAKE2b-256 | aa8d50c26253e2cf3c8fd26ecf78efa3773aa160c453a1ea320cfedb935ad77a |
File details
Details for the file jinja2_iso8601-1.0.0-py2.py3-none-any.whl
.
File metadata
- Download URL: jinja2_iso8601-1.0.0-py2.py3-none-any.whl
- Upload date:
- Size: 2.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.27.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0073a7b5ef39c9d9e355527c0ce44c1bb5e81f16fa5ccecd703ea1a024d6b1a6 |
|
MD5 | a5e8b7f04e7258c663bdb7fe96ac9ac3 |
|
BLAKE2b-256 | c8645be21f41fa1e1606792002d3361f4484ac23aeedc20d1507d471f11270c8 |