Jinja2-time is a Jinja2 extension that provides a `now` tag for convenient access to date and time functionalities directly within Jinja2 templates. It allows retrieving the current time in various timezones, formatting it using Python's `strftime` patterns, and applying relative time offsets. The current version is 0.2.0, released in June 2016. It relies on Jinja2 and `arrow` for robust date and time handling.
pip install jinja2-timeVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize a Jinja2 environment with `jinja2-time` and use the `now` tag to display the current time in various timezones, with custom formatting, and with relative time offsets. It also shows how to set a default datetime format for the environment.
Pin your Jinja2 dependency to `<3.0` if you encounter compatibility issues, or consider implementing custom Jinja2 filters/extensions for date/time functionality if an upgrade to Jinja2 3.x is critical.
Explicitly define the desired timezone in the `{% now 'timezone' %}` tag, e.g., `{% now 'utc' %}` or `{% now 'America/New_York' %}`.Refer to the `arrow.Arrow.replace` documentation for valid arguments and ensure the offset string adheres to the `'key=value,key2=value2'` format (e.g., `{% now 'utc' + 'years=1,months=-3,days=15' %}`).Consult Python's official `strftime` documentation for a comprehensive list of format codes and their meanings to ensure correct date and time string representation.
Install the library using pip: `pip install jinja2-time`
When creating your Jinja2 Environment, include 'jinja2_time.TimeExtension' in the `extensions` list: `env = Environment(extensions=['jinja2_time.TimeExtension'])`
Ensure that time offsets are correctly formatted (e.g., `'hours=2, seconds=30'`) and that format strings adhere to Python's `strftime` patterns, enclosed in quotes. For example: `{% now 'utc' + 'hours=2', '%Y-%m-%d %H:%M:%S' %}`Specify the desired timezone as the first argument to the `{% now %}` tag. For example, to display local time, use `{% now 'local' %}`, or for a specific timezone like New York, use `{% now 'America/New_York' %}`.