Python Django Get Current Date Example

Published On: 14/07/2022 | Category: Django Python

Hi Dev,

This tutorial will provide example of python django get current date. This article will give you simple example of python django get current date by time. we will help you to give example of python django get current date and time. you can understand a concept of python django get current date datetime.

So, in this post we will use HttpResponse for render a html the view returns an HttpResponse object that contains the generated response. each view function is responsible for returning an HttpResponse object.

The easiest ways 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.

Let's see bellow example python django get current date from datetime.

Example : 1

In this step, we need to create the views for performing get today date.Open the views.py file and add:

views.py
from django.shortcuts import render
import datetime
from django.http import HttpResponse

# Create your views here.
def current_date(request):
    current_date = datetime.date.today()  
    html = "<html><body><b>Today\'s Date:</b> %s</body></html>" % current_date
    return HttpResponse(html)
Output
Today's Date: 2022-07-14
Example : 2 views.py
from django.shortcuts import render
import datetime
from django.http import HttpResponse

# Create your views here.
def current_datetime(request):
    current_datetime = datetime.datetime.now()
    html = "<html><body><strong>Current Date and Time Value:</strong> %s</body></html>" % current_datetime
    return HttpResponse(html)
Output
Current Date and Time Value: 2022-07-14 09:34:10.096782

I Hope It will help you....