Skip to content

Attempt to see if possible to port moment.js to day.js for fun.

License

Notifications You must be signed in to change notification settings

kingkong-cmd/Flask-Dayjs

 
 

Repository files navigation

Flask-Dayjs

This extension enhances Jinja2 templates with formatting of dates and times using day.js.

Installation

Install the extension with uv:

uv add git+https://github.com/kingkong-cmd/Flask-Dayjs.git

Initialization

Once 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() }}

Usage

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>

Resources

About

Attempt to see if possible to port moment.js to day.js for fun.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 92.9%
  • HTML 7.1%