How to Get the Current Time in Django?

When I first started working with Django, one of the common tasks I encountered was fetching the current time. Whether it was for logging user activity, timestamping records, or scheduling tasks, knowing how to get the current time efficiently and correctly is essential.

In this guide, I’ll share the easy methods I use daily to get the current time in Django projects. These methods cover everything from using Python’s built-in modules to leveraging Django’s timezone utilities. By the end, you’ll be confident in handling time-related data in your Django applications.

Methods to Get the Current Time Matter in Django

Most web applications require timestamps for user actions, data creation, or even scheduling events. Django, being a high-level Python web framework, provides robust tools to handle date and time efficiently.

However, working with time can be tricky, especially when considering time zones and daylight saving time. That’s why I always recommend using Django’s timezone-aware utilities to avoid common pitfalls.

Read Python Django Random Number

Method 1: Use Python’s datetime Module

The simplest way to get the current time is by using Python’s built-in datetime module. This method is easy and works well when you don’t need to handle time zones explicitly.

Here’s how I do it:

from datetime import datetime

current_time = datetime.now()
print(current_time)

I executed the above example code and added the screenshot below.

django get current time

This returns the current local date and time based on the server’s clock. However, if your Django project serves users across different time zones (like across the USA), this might not be the best option because it doesn’t handle time zones.

Check out Python Django Format Date

Method 2: Use Django’s timezone Module for Timezone-Aware Time

Django comes with a timezone module that helps you get the current time with time zone awareness. This is crucial for applications that serve users in multiple time zones or where daylight saving time changes matter.

Here’s how I use it:

from django.http import HttpResponse
from datetime import datetime

def index(request):
    current_time = datetime.now()
    return HttpResponse(f"<h2>Current Time: {current_time}</h2>")

I executed the above example code and added the screenshot below.

django datetime now

This method returns the current time as a timezone-aware datetime object, which is usually in UTC by default unless you configure your TIME_ZONE settings in Django.

Pro tip: Always store timestamps in UTC in your database and convert them to the user’s local time when displaying.

Read Django: Get All Data From POST Request

Method 3: Get the Current Date or Time Separately

Sometimes, you might only need the current date or just the time part. Here’s how to do that using Django’s timezone:

from django.utils import timezone

current_date = timezone.now().date()
current_time = timezone.now().time()

print(f"Date: {current_date}, Time: {current_time}")

I executed the above example code and added the screenshot below.

django get current date

This is handy when you want to display or work with just the date or time in your application logic.

Check out Convert HTML Page to PDF using Django in Python

Method 4: Format the Current Time for Display

Raw datetime objects are not always user-friendly. When building user interfaces, especially for an American audience, formatting the date and time familiarly is important.

Here’s how I format the current time:

from django.utils import timezone

now = timezone.now()
formatted_time = now.strftime('%m/%d/%Y %I:%M %p')  # e.g., 07/21/2025 03:45 PM

print(formatted_time)

This gives you a string formatted as MM/DD/YYYY HH:MM AM/PM which is the common format used in the USA.

Read Get URL Parameters in Django

Method 5: Use Timezone Settings for Local Time

If you want to get the current time in a specific timezone (for example, Eastern Time in the USA), you can use the pytz library with Django’s timezone utilities.

First, make sure you have pytz installed:

pip install pytz

Then, use it like this:

from django.utils import timezone
import pytz

eastern = pytz.timezone('US/Eastern')
current_time_eastern = timezone.now().astimezone(eastern)

print(current_time_eastern.strftime('%m/%d/%Y %I:%M %p %Z'))

This will print the current time in Eastern Time, including the timezone abbreviation (e.g., EDT or EST).

Check out Python Django Length Filter

Method 6: Get Current Time in Templates

Sometimes, you want to display the current time directly in your Django templates.

You can pass the current time from your view:

from django.utils import timezone
from django.shortcuts import render

def my_view(request):
    current_time = timezone.now()
    return render(request, 'my_template.html', {'current_time': current_time})

Then, in your template, you can format it using Django’s template filters:

Current time: {{ current_time|date:"m/d/Y h:i A" }}

This approach keeps your templates clean and leverages Django’s built-in date formatting.

Read Django for Loop

Best Practices When Working with Time in Django

  • Always use timezone-aware datetime objects. This helps avoid bugs related to daylight saving time and user location.
  • Store all timestamps in UTC in your database.
  • Convert to the user’s local time only when displaying data.
  • Use Django’s timezone.now() instead of Python’s datetime.now() to maintain consistency.
  • Format dates and times according to your audience’s locale. For the USA, the MM/DD/YYYY format is standard.

Getting the current time in Django is simpler than it looks once you understand the tools available. Whether you’re building an app that logs user actions or schedules events across different US time zones, these methods ensure your time data is accurate and user-friendly.

If you’re new to Django or even a seasoned developer, using Django’s timezone utilities will save you headaches down the road. I hope this guide makes it easier for you to handle time in your Django projects.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.