If you’ve worked with Django for a while, you know that handling dates and times is a common task. Whether you’re displaying timestamps on your website or processing form data, converting DateTime objects to strings in a specific format is essential.
I’ve found that mastering DateTime formatting not only improves the user experience but also helps maintain clean and readable code. In this article, I will share simple, effective ways to convert DateTime objects to strings in Django, complete with real-world examples.
Let’s get right in!
Method to Convert DateTime to a String in Django
Django’s DateTimeField stores date and time as Python datetime objects. While these objects are powerful for manipulation, they are not always suitable for displaying directly to users.
For example, if you want to show a date like “March 15, 2024, 2:30 PM” on a webpage, you need to convert the datetime object to a formatted string. This makes your app’s UI friendly and consistent.
Read AttributeError: Module Object has no Attribute in Django
Method 1: Use Python’s strftime() Method
The easy way to convert a DateTime object to a string is using Python’s built-in strftime() method. It allows you to specify the exact format you want.
Here’s how I typically use it in Django views or templates:
from datetime import datetime
# Example datetime object
now = datetime.now()
# Convert to string in the format: Month Day, Year Hour:Minute AM/PM
formatted_date = now.strftime("%B %d, %Y %I:%M %p")
print(formatted_date) # Output: March 15, 2024 02:30 PMExplanation:
%B– Full month name (e.g., March)%d– Day of the month (e.g., 15)%Y– Four-digit year (e.g., 2024)%I– Hour (12-hour clock)%M– Minutes%p– AM/PM
This method works great in views when you want to prepare data before sending it to the template.
Use strftime() in Django Views Example
Here’s a simple Django view that fetches the current date and sends the formatted string to the template:
from django.shortcuts import render
from datetime import datetime
def show_date(request):
now = datetime.now()
formatted_date = now.strftime("%B %d, %Y %I:%M %p")
context = {'formatted_date': formatted_date}
return render(request, 'date_template.html', context)And in your template date_template.html:
<p>Current Date and Time: {{ formatted_date }}</p>This will display:
Current Date and Time: July 31, 2025 06:19 PMYou can refer to the screenshot below to see the output.

Method 2: Use Django’s Built-in Template Filters
If you prefer to handle formatting directly in the template, Django provides built-in filters like date and time.
Assuming you pass a DateTime object to the template, you can format it like this:
<p>Current Date and Time: {{ now|date:"F d, Y h:i A" }}</p>Here’s how you might pass now from your view:
from django.shortcuts import render
from datetime import datetime
def show_date(request):
now = datetime.now()
context = {'now': now}
return render(request, 'date_template.html', context)The filter "F d, Y h:i A" formats the date as:
F– Full month named– Day of the monthY– Four-digit yearh– Hour (12-hour clock)i– MinutesA– AM/PM
This method keeps your Python code clean and leverages Django’s powerful templating system.
You can refer to the screenshot below to see the output.

Check out Delete Session Property in Django
Method 3: Use Python’s isoformat() Method for ISO 8601 Strings
Sometimes, especially when working with APIs or JavaScript frontends, you might want to convert a DateTime to an ISO 8601 string, which looks like "2024-03-15T14:30:00".
Here’s how:
from datetime import datetime
now = datetime.now()
iso_string = now.isoformat()
print(iso_string) # Output: 2024-03-15T14:30:00.123456If you want to remove microseconds:
iso_string = now.replace(microsecond=0).isoformat()
print(iso_string) # Output: 2024-03-15T14:30:00You can refer to the screenshot below to see the output.

This format is great for JSON responses, AJAX calls, or when working with JavaScript’s Date object.
Method 4: Custom Template Filter for Reusable Formatting
If you want to reuse a specific DateTime format across your project, creating a custom template filter is a clean approach.
Here’s how I create one:
- Create a new file
templatetags/datetime_filters.pyinside your app folder. - Add the following code:
from django import template
register = template.Library()
@register.filter
def format_datetime(value, fmt="%B %d, %Y %I:%M %p"):
if value:
return value.strftime(fmt)
return ""- Load and use it in your template:
{% load datetime_filters %}
<p>Formatted Date: {{ now|format_datetime:"%m/%d/%Y, %H:%M" }}</p>This filter lets you specify any format on the fly and keeps your templates neat.
Read Django CRUD with Bootstrap Template
Tips for Working with Time Zones in Django
If your project handles users across different US time zones (e.g., EST, PST), remember to use Django’s timezone utilities to avoid confusion.
from django.utils import timezone
now = timezone.localtime(timezone.now())
formatted_date = now.strftime("%B %d, %Y %I:%M %p %Z")
print(formatted_date) # Output: March 15, 2024 02:30 PM ESTThis ensures your displayed times are accurate for your users.
Working with DateTime objects in Django doesn’t have to be complicated. Whether you want quick formatting in views, elegant handling in templates, or reusable custom filters, these methods cover most use cases.
Choose the method that best fits your project style and requirements.
If you have any questions or want to share your tips, feel free to leave a comment below!
I hope you found this tutorial helpful. Happy coding!
Related Articles You May Like:

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.