Django - present current date and time in template
Asked Answered
I

2

52

How can one present current data and time in django's template?

Ingathering answered 14/4, 2015 at 21:31 Comment(0)
B
100

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" %}
Berkin answered 14/4, 2015 at 21:33 Comment(4)
There is an option to translate this built-in templates ?Backfire
@Backfire modify the language in settings.pyMendes
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
C
3

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.
Chorion answered 28/9, 2023 at 0:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.