How to Format Dates in Python Django

I’ve often found that handling dates and times in Django projects is a common yet sometimes tricky task. Formatting dates to display them properly on your website is essential for user experience, especially when your audience is in the USA, where date formats differ from other regions.

In this article, I’ll share practical methods to format dates in Django. Whether you want to format dates in your views or directly in your templates, I’ll walk you through clear examples that you can implement right away.

Let’s get started!

Methods to Format Dates in Django

Dates are everywhere in web apps, from blog posts to event schedules. But raw date objects aren’t always user-friendly. For example, a Python datetime object might display as 2025-07-23 14:35:00, which isn’t very readable for most users.

In the USA, the common date format is MM/DD/YYYY or sometimes, a more verbose format like July 23, 2025. Making sure your Django app displays dates in a familiar format improves clarity and professionalism.

Read Payment Gateway Integration with Django

Method 1: Format Dates in Django Views Using Python’s strftime()

The first method I use is formatting dates directly in the view before sending them to the template. Python’s built-in strftime() function is very handy here.

Example: Formatting Date in View

from django.shortcuts import render
from datetime import datetime

def event_view(request):
    event_date = datetime(2025, 7, 23, 14, 35)
    # Format date as MM/DD/YYYY
    formatted_date = event_date.strftime('%m/%d/%Y')
    context = {
        'event_date': formatted_date
    }
    return render(request, 'event.html', context)

You can see the output in the screenshot below.

django date format ddmmyyyy

In this example, I created a datetime object for July 23, 2025, at 2:35 PM. Using strftime('%m/%d/%Y'), I formatted it to 07/23/2025 which is a common US format.

You can then display {{ event_date }} in your template as a simple string.

Check out How to Add Items to Cart in Django in Python?

Method 2: Format Dates Directly in Django Templates Using the date Filter

If you prefer to pass the raw datetime object to the template and format it there. Django’s built-in date template filter is perfect.

Example: Using date Filter in Template

View:

from django.shortcuts import render
from datetime import datetime

def event_view(request):
    event_date = datetime(2025, 7, 23, 14, 35)
    context = {
        'event_date': event_date
    }
    return render(request, 'event.html', context)

Template (event.html):

<p>Event Date: {{ event_date|date:"m/d/Y" }}</p>

This will output:

Event Date: 07/23/2025

You can see the output in the screenshot below.

django date format

The date filter uses the same formatting codes as Python’s strftime(). For example:

  • m – Month as zero-padded decimal (01 to 12)
  • d – Day of the month (01 to 31)
  • Y – Year with century (e.g., 2025)

Read Check if Python Dictionary is Empty

Method 3: Use Custom Date Formats in Django Settings

If you want consistent date formatting across your entire Django project, you can define your preferred format in the settings.

Add this to your settings.py:

DATE_FORMAT = 'N j, Y'  # Example: Jul 23, 2025

Then in your template, simply use:

<p>Event Date: {{ event_date|date }}</p>

You can see the output in the screenshot below.

django template date format

Django will use the DATE_FORMAT you specified.

Check out Python Filter Not in Django

Method 4: Format Dates with Localization (For US English)

Django supports localization, which means it can automatically format dates according to the locale.

To enable US English formatting:

  1. In settings.py, set:
LANGUAGE_CODE = 'en-us'
USE_L10N = True
  1. Use the localize template filter in your template:
{% load l10n %}
<p>Event Date: {{ event_date|localize }}</p>

This will format the date according to US locale settings, which usually means MM/DD/YYYY.

Method 5: Format Dates in Django Forms and Models

If you’re working with forms or models and want to control date input/output formats, you can specify formats using Django’s DateField and widgets.

Example: Custom Date Input Format in a Form

from django import forms

class EventForm(forms.Form):
    event_date = forms.DateField(
        input_formats=['%m/%d/%Y'],
        widget=forms.DateInput(format='%m/%d/%Y')
    )

This ensures the form accepts and displays dates in the US format.

Read Union Operation on Django Models

Conclusion

I’ve shown you several ways to format dates in Django, from formatting in views with Python’s strftime(), to using Django’s template filters, localization, and even form input formatting.

Each method has its use case. Formatting in views is good for simple display needs. Template filters keep your views clean and allow for flexible formatting. Localization is essential if you want to support different regions but still prioritize US formatting. And form formatting ensures user input matches expected formats.

With these tools, you can handle dates in your Django apps confidently and provide a polished experience for your users.

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.