In this Django tutorial, we will understand How to get the current time in Django. And we will also understand How to get the current date as well as a Datetime value. Also, discuss how to use them in Django. Here is the list of topics that we are going to cover.
- Get current datetime in django
- Get current time in django
- Get current date in django
- Get current time in django template
- Get current date in django template
- Get current Datetime in django template
Get current Datetime in Django
The simplest way to fetch the current DateTime value in Django is by using python’s datetime module. This datetime module contains many different classes that can be used to manipulate date and time values.
We can easily use these classes by importing the datetime module in the views.py file of our Django application.
Now, to fetch the current DateTime value, we will use the datetime class available in the datetime module. Let’s understand the implementation by following the given steps.
- 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.
- Now, we can either assign this method to a variable or we can directly use this method wherever we required the datetime value.
Now, let’s execute an example based upon these steps and understand the implementation. For this, we have added the following code in our views.py file.
from django.http import HttpResponse
import datetime
def index(request):
current_datetime = datetime.datetime.now()
html = "<html><body><b>Current Date and Time Value:</b> %s</body></html>" % current_datetime
return HttpResponse(html)
In the example, we have imported the datetime module and used its datetime.now() method to store the Datetime value in a variable. And then, we are using the variable in an HttpResponse.
Now, if we start the development server and move to the mapped URL, we will get the following result.
Read: Python Django vs Flask
Get current time in Django
Now, what if we only want the current time value in the Django application. So, in this section, we will understand how to fetch the current time in Django.
There are multiple ways to fetch and use the current time value in the application. And we will try to understand most of them in this section.
- Using datetime class
- Using time module
Using datetime class
Now, in this approach, we will use the datetime class which is there in the datetime class. And there are two methods to get the current time using it.
In the first method, we will use the datetime.strftime() method to create a string representing the current time. Here is an example of using the strftime() method in the views.py file.
from django.http import HttpResponse
from datetime import datetime
def index(request):
current_time = datetime.now().strftime('%H:%M:%S')
html = "<html><body><b>Current Time Value:</b> %s</body></html>" % current_time
return HttpResponse(html)
In the above example, we have imported the datetime class from the datetime module. Next, we have created a view with a name index. In this view, we are using the datetime.strftime() method to store the current time value in a variable. And then, we are using the variable to return the current time value as an HTTP response.
Now, if we run the development server and move to the mapped URL, we will get the following output.
In the second method, we will create a time object to fetch the current time. For this, we will use the datetime.now().time() method and it will change the DateTime value to only time value. Here is how we can create a time object in the views.py file.
from django.http import HttpResponse
from datetime import datetime
def index(request):
current_time = datetime.now().time()
html = "<html><body><b>Current Time Value:</b> %s</body></html>" % current_time
return HttpResponse(html)
Now, if we start the development server and open the URL, we will get the following result.
Read Python Django get
Using time module
Now, in this section, we will understand how to use the time module to get the current time value in Django.
The time module is a python module that contains various functions related to time. Let’s understand how we can use this with the help of the following example.
from django.http import HttpResponse
import time
def index(request):
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
html = "<html><body><b>Current Time Value:</b> %s</body></html>" % current_time
return HttpResponse(html)
In the above example, first, we have imported the time module and then, we are creating a view. in the view, we have defined a variable “t” to get the current time.
After this, we are using the strftime() method to format the time value. In the end, we will return the time value using HTTP response. Here is the result of the above example.
Read: How to install Django
Get current date in Django
Till now, we have discussed how to fetch current DateTime and time values. Now, in this section, we will discuss how to get current date in Django.
Now, there are many methods to get the current date in python, and we can use the same to get the current date in Django.
The easiest way to get the current date is by using the datetime module. And there are 2 ways to use the datetime module to fetch the current date.
The first way is to use the date.today() method and this method returns the current date. Here is an example of using the date.today() method in the views.py file.
from django.http import HttpResponse
import datetime
def index(request):
current_date = datetime.date.today()
html = "<html><body><b>Today\'s Date:</b> %s</body></html>" % current_date
return HttpResponse(html)
In the example, first, we have imported the datetime module. After this, we are using date.today() method to store the current date in the variable with the name current_date. In the end, we are returning the date as an HTTP response.
Now, if we start the development server and open the URL, we will get the following result.
The second method is by using the datetime class from the datetime module. In this, we have to use the datetime.now().date() method to get the current date. Here is an example of using datetime.now().date() method in Django.
from django.http import HttpResponse
from datetime import datetime
def index(request):
current_date = datetime.now().date()
html = "<html><body><b>Today\'s Date:</b> %s</body></html>" % current_date
return HttpResponse(html)
In the example, we have imported the datetime class from the datetime module. Next, we are using the datetime.now().date() method to store the current date value in a variable.
In the end, we are returning the variable as a response. And this example will also return the same result as shown above.
Read: How to setup Django project
Get current time in django template
Till now, we were only discussing fetching the current date and time in the views file. Now, we will understand how to get the current date and time in the Django templates.
In Django, there are 2 methods to get the current time in the templates. The first method is to get the current time in the views.py file and send it to the template page.
And then, use it in the template to get the output. The second method is by using Django template tags to fetch the current time.
In this section, we will discuss both methods by illustrating some examples.
Using Views file
In this first method, we will use the views.py to fetch the current time and then, send it to the template. For this, we have added the following code in our views.py file.
from django.shortcuts import render
from datetime import datetime
def index(request):
current_time = datetime.now().time()
return render(request, 'index.html', {'current_time':current_time})
In the above code, we have created a function-based view with the name index. And in this view, we have defined a variable that will store the value of the current time using datetime.now().time() method. And then, we have sent the variable to the index.html page.
Now to display the current time using the variable, we have to add the following code to the index.html page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Python Guides</title>
</head>
<body>
<b>Current Time: </b>{{current_time}}
</body>
</html>
Now, if we start the development server and open the URL, we will get the following result.
Using Template Tags
In this approach, we will simply create a view that will redirect to an HTML page. After this, in the template, we will use the “now” tag to get the current time. First, the code of the views.py file is as follows.
from django.shortcuts import render
def index(request):
return render(request, 'index.html')
Next, we will add the following code to the index.html file to get the result.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Python Guides</title>
</head>
<body>
<b>Current Time: </b>{% now "P" %}
</body>
</html>
In the above code, the {% now “P” %} tag will return the current time value. Here is the output of the above example.
Read: Difference between app and project in Django
Get current date in django template
So, similar to the previous section, we can use 2 different methods to get the current date in the Django template. And in this section also, we will discuss both methods in case of the current date.
Using Views file
For the first method, we will use the views.py to fetch the current date value and then, send the date value to the template. For this implementation, we have to add the following code in the views.py file.
from django.shortcuts import render
from datetime import datetime
def index(request):
current_date = datetime.now().date()
return render(request, 'index.html', {'current_date':current_date})
In the above code, we have created a function-based view and in this view, we have defined a variable. Now, this variable will store the value of the current date using datetime.now().date() method.
And then, we have sent the variable to the index.html page using the return statement.
Next, to display the current date, we have to add the following code to the index.html page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Python Guides</title>
</head>
<body>
<b>Today's Date: </b>{{current_date}}
</body>
</html>
In the above example, the “{{current_date}}” variable will return the current date. And here is the output of the above page.
Using Template Tags
In this method, first, we will create a view that will redirect to an HTML page. After this, in the template, we will use the “now” tag to get the current date. First, the code of the views.py file is as follows.
from django.shortcuts import render
def index(request):
return render(request, 'index.html')
Next, we will add the “now” tag to the index.html file to get the result. Also, we can have the current date in different formats using this tag.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Python Guides</title>
</head>
<body>
<p><b>Today's Date: </b>{% now "d-m-Y" %}</p>
<p><b>Today's Date: </b>{% now "d/m/Y" %}</p>
<p><b>Today's Date: </b>{% now "l, j F Y" %}</p>
</body>
</html>
Now, start the development server and open the mapped URL in the browser. We will get the following result.
Read: Python Django get admin password
Get current Datetime in django template
In this section, we will discuss how to get a current DateTime value in the Django template.
For this implementation, we will discuss 2 different methods of fetching a current DateTime value and using it in the template. And both the methods are as follows.
- First, use the view.py file to fetch the datetime value and send it to the html page. After this, use the datetime value in the HTML page to get the result.
- Second, create a view that redirects to the HTML page and then, use template tags in the HTML page to get the datetime value.
Using Views file
Let’s understand this approach by executing an example in Django. And for this example, first, we will add the following code in the views.py file.
from django.shortcuts import render
from datetime import datetime
def index(request):
current_datetime = datetime.now()
return render(request, 'index.html', {'current_datetime':current_datetime})
In the views.py file, we have created a simple view that fetches the current DateTime value into a variable. And it also returns the variable to the index.html page.
Now, for the index.html page, we will add the following code.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Python Guides</title>
</head>
<body>
<p><b>Current Date-Time: </b>{{current_datetime}}</p>
</body>
</html>
In the index.html page, we are just using the variable to get the current DateTime value. In the end, we will get the following output.
Using Template Tags
In this approach, we will simply need to create a view that redirects to an HTML page. And here is the code for the view.
from django.shortcuts import render
def index(request):
return render(request, 'index.html')
Now, by using the template tags, we can get the current DateTime values in different formats. Some common formats are shown in the example below.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Python Guides</title>
</head>
<body>
<p><b>Current Date-Time: </b>{% now "SHORT_DATETIME_FORMAT" %}</p>
<p><b>Current Date-Time: </b>{% now "jS F Y, H:i A" %}</p>
<p><b>Current Date-Time: </b>{% now "c" %}</p>
<p><b>Current Date-Time: </b>{% now "r" %}</p>
</body>
</html>
And here is the output of the above example, when we run the development server.
Also, read the following Django tutorials:
- Django get all data from POST request
- How to Create model in Django
- Python Django filter
- Python Django group by
- Convert HTML page to PDF using Django in Python + PDF Invoice
In this Django tutorial, we discussed How to get the current time in Django, How to get the current date as well as a Datetime value. Also, we discussed the following list of topics.
- Get current datetime in django
- Get current time in django
- Get current date in django
- Get current time in django template
- Get current date in django template
- Get current Datetime in django template
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.