Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - present current date and time in template

Tags:

python

django

The title is pretty self explanatory. How can one present current data and time in django's template?

like image 551
Neithrik Avatar asked Apr 14 '15 21:04

Neithrik


People also ask

How to display current date in Django template?

First, open the views.py file of your Django application and import the datetime module. Next, use the datetime. now() method to get the current date and time value.

How do I get current year in Django?

The full tag to print just the current year is {% now "Y" %} . Note that the Y must be in quotes. For the record, {% now %} won't give proper results for users within different time zone than the server.

What is Django template language?

A Django template is a text document or a Python string marked-up using the Django template language. Some constructs are recognized and interpreted by the template engine. The main ones are variables and tags. A template is rendered with a context.


1 Answers

Try using built-in django template tags and filters: https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#now

Examples:

It is {% now "jS F Y H:i" %}
It is {% now "SHORT_DATETIME_FORMAT" %}
like image 200
Sara Avatar answered Oct 16 '22 14:10

Sara