How can one present current data and time in django's template?
Django - present current date and time in template
Try using built-in django template tags and filters: https://docs.djangoproject.com/en/stable/ref/templates/builtins/#now
Examples:
It is {% now "jS F Y H:i" %}
It is {% now "SHORT_DATETIME_FORMAT" %}
There is an option to translate this built-in templates ? –
Backfire
@Backfire modify the language in settings.py –
Mendes
To complement this answer: if you need the current date as a variable (
date
or datetime
instead of just a str
) you can find a few options in this answer I wrote some time ago. –
Highly can this display days like Sunday to Monday? –
Nicholas
The full list of datetime formats can be found in the docs. Using the format options there, you can construct the current datetime in whatever format you want. For example,
It is {% now "l, F j, Y g:i A" %}.
displays
It is Wednesday, September 27, 2023 11:58 PM.
on the webpage, and
It is {% now "" %}.
It is {% now "N j, Y" %}.
both display
It is Sept. 28, 2023.
© 2022 - 2024 — McMap. All rights reserved.