📝 Django

Timezones in Django

P
Author
Pyland
📅
Published
30.06.2026
⏱️
Reading time
1 min
👁️
Views
70
🌿
Level
Medium

Django stores all DateTimeField values in UTC when USE_TZ=True. Conversion to local time happens at the display layer.

Configuration

# settings.py
USE_TZ = True                    # enable timezone support (recommended)
TIME_ZONE = 'Europe/Moscow'      # server timezone

Aware vs Naive Datetime

from django.utils import timezone
from datetime import datetime

# Aware — datetime with timezone info (correct when USE_TZ=True)
now = timezone.now()  # UTC datetime with tzinfo

# Naive — without timezone info (dangerous!)
naive = datetime.now()  # local time without tzinfo

Working with Dates

from django.utils import timezone

# Current time (UTC)
now = timezone.now()

# Current date in the local timezone
today = timezone.localdate()

# Convert to local time
local_now = timezone.localtime(now)

# Convert an aware datetime
from django.utils.timezone import make_aware
import pytz

moscow = pytz.timezone('Europe/Moscow')
aware = make_aware(datetime(2024, 1, 15, 12, 0), moscow)

Storage in the Database

Django stores all DateTimeField values in UTC when USE_TZ=True. Conversion to local time happens at the display layer.

In Templates

{# Automatically converts to the local timezone #}
{{ task.created_at|date:"d.m.Y H:i" }}

{# Force a specific timezone in the template #}
{% load tz %}
{% timezone "Europe/Moscow" %}
  {{ task.created_at }}
{% endtimezone %}

Switching Timezone per User

from django.utils import timezone

def set_timezone(request):
    if request.method == 'POST':
        tz = request.POST.get('timezone')
        request.session['django_timezone'] = tz
    return redirect('/')
# middleware
class TimezoneMiddleware:
    def __call__(self, request):
        tzname = request.session.get('django_timezone')
        if tzname:
            timezone.activate(pytz.timezone(tzname))
        else:
            timezone.deactivate()
        return self.get_response(request)

Your reaction to the article

💬 Comments (0)

🔐 Sign in to leave a comment
🚪 Login
💭

No comments yet

Be the first to share your opinion about this article!

🔗 Similar

Similar articles

Continue learning with these materials

📝

pytest-django: Testing Django

Охватываемые темы: Installation, @pytest.mark.djangodb, Fixtures, Testing views.

📅 30.06.2026 👁️ 138
📝

AI Agents: ReAct Loop and Autonomous Actions

A chatbot answers questions. An agent takes action: it calls tools, retrieves real data, and...

📅 30.06.2026 👁️ 103
📝

RAG: Chatting with Documents via Vector Search

RAG (Retrieval-Augmented Generation) is a pattern for working with your own documents. Instead of fine-tuning...

📅 30.06.2026 👁️ 94

Did you like the article?

Subscribe to our updates and receive new articles first. Grow with PyLand!