This extension enhances Jinja2 templates with formatting of dates and times using day.js.
Install the extension with uv:
uv add git+https://github.com/kingkong-cmd/Flask-Dayjs.gitOnce the extension is installed, create an instance and initialize it with the Flask application:
from flask_dayjs import Dayjs
dayjs = Dayjs(app)To complete the initialization, add the following line inside the <head>
section of the template(s) that will use the extension:
{{ dayjs.include_dayjs() }}To render a date with Flask-Dayjs, use the dayjs() function with one of
the available formatting options. The following example uses a custom format
string to render the current time:
<p>Current date and time: {{ dayjs().format('MMMM D YYYY, h:mm:ss a') }}.</p>To render a timestamp other than the current time, pass a datetime object
as an argument to the dayjs() function:
<p>Date: {{ dayjs(date).format('LL') }}</p>Some of the rendering functions render the elapsed time between a datetime object and current time. The following example renders a timestamp in a "time ago" style:
<p>{{ author }} said {{ dayjs(comment_datetime).fromNow() }}</p>