In this Django Tutorial, we will understand How to set time zone in python Django. And we will also discuss examples related to the time zone in Django. Here is the list of topics that we are going to cover.
- Python Django set timezone
- Python Django set timezone settings
- Python Django set timezone UTC
- Python Django set timezone to EST
- Python Django set timezone to IST
- Python Django set timezone for user
Python Django set timezone
A time zone is just a region of the globe where everyone is on the same clock. However, due to something called daylight savings time, a given time zone may not follow the same time throughout the year.
Because of these issues, the globe agreed to adopt a universal time standard that remains constant regardless of where you are or what time of year it is. The coordinated universal time, or UTC, is the name for this.
For example, I live in India, which is 5 hours and 30 minutes ahead of UTC, so the time was 10:32:10 PM UTC+5:30 at this time. 10:32:10 PM GMT+5:30 is another way to write it. There is no difference between GMT and UTC in terms of time; nonetheless, GMT is a time zone, and UTC is a time measurement standard.
Also, check: How to Get Current time in Django
Python Django set timezone settings
For saving information in the database, Django recommends using UTC. Even if your website is only accessible in a one-time zone, storing data in UTC in your database is still a good strategy. Daylight Saving Time is the primary reason for this.
DST is a time-keeping system in which clocks are pushed forward in the spring and backward in the autumn in many nations. If you work in local time, you’re likely to run into problems twice a year when the clocks change.
You only need to configure a few things in your Django application’s settings.py to set the timezone.
TIME_ZONE = '<Time zone of your choice>'
USE_TZ = True
By default, the Time zone is UTC, and USE_TZ is set to True, which ensures that using a datetime.now() function in your Django application creates time in UTC.
Read: How to Create model in Django
Python Django set timezone UTC
Now, in this section, we will discuss how to set the timezone to UTC. For the demonstration of this topic, we are using the Delivery model in Django. And the model contains the following data.
Django’s timezone is set to UTC by default. If your timezone is not UTC, you can change it using the procedure below:
- Open the setting.py file in the directory of our project.
- Internationalization can be found by scrolling down the file.
- TIME ZONE will appear under this.
- Set it to ‘UTC’.
Command to set timezone:
TIME_ZONE='UTC'
Now you may start the server and log in to the admin interface. After that, open your Delivery model, you’ll notice that the time has been changed to UTC.
Read: Python Django get admin password
Python Django set timezone to EST
Now, in this section, we will discuss how to set the timezone to EST. To illustrate this topic, we are using the Delivery model in Django. And the model contains the following data and here the time zone used in this model is UTC, by default.
To set the timezone, you must first set up a few things in your Django application’s settings.py, which are as follows:
Command to set timezone:
TIME_ZONE = 'EST'
You can now start the server and access the administrative interface. When you open your Delivery model, you’ll see that the time has been changed to EST.
Here is the output, which clearly shows that the time zone changes to EST.
Read: If statement in Django template
Python Django set timezone to IST
Now, in this section, we will discuss how to set the timezone to IST. To illustrate this topic, we are using the Delivery model in Django. And the model contains the following data and here the time zone used in this model is UTC, by default.
Here IST means Indian Standard Time, which is Asia/Kolkata as per the time zone. And, to set the timezone, you must first set up a few things in your Django application’s settings.py, which are as follows:
Command to set timezone:
TIME_ZONE = 'Asia/Kolkata'
You can now start the server and access the administrative interface. When you open your Delivery model, you’ll see that the time has been changed to IST.
Here is the output, which clearly shows that the time zone changes to IST.
Read: Get URL parameters in Django
Python Django set timezone for user
In this section, we’ll learn how to set the timezone for users based on their local timestamp. If you want to create timestamps in the local time zone. Do the following settings in the settings.py file of the Django project.
USE_TZ = FALSE
If you set USE_TZ to False, Django assumes your application doesn’t care about time zones. It refers to the location where the Django application is hosted; for example, if you run it locally on your computer, it will use that time.
Let’s see an example to understand the concept clearly:
By default, the time zone is set to ‘UTC’, and the use time zone is set to ‘True’. So, when we fetch the time it shows the time in UTC.
The views.py file:
from django.http import HttpResponse
from django.utils import timezone
# Create your views here.
def home(request):
current_time = timezone.now().strftime('%H:%M:%S')
html = "<html><body><b>Current Time Value:</b> %s</body></html>" %
current_time
return HttpResponse(html)
In the above example, we have imported the timezone class from the django.utils module. Next, we have created a view with the name home. In this view, we are using the timezone.now().strftime() method to store the current time value in a variable. And then, we are using the variable to return the current time value as an HTTP response.
Now, if we run the development server and move to the mapped URL, we will get the following output.
Now, if you set the time zone to ‘UTC’ and use the time zone is set to ‘False’. It will show time as per the local time zone. In this example, the local time zone is Asia/Kolkata.
The views.py file:
from django.http import HttpResponse
from django.utils import timezone
# Create your views here.
def home(request):
current_time = timezone.now().strftime('%H:%M:%S')
html = "<html><body><b>Current Time Value:</b> %s</body></html>" %
current_time
return HttpResponse(html)
Now, if we run the development server and move to the mapped URL, we will get the following output.
You may also like to read the following Django tutorials.
- Python list append Django
- Python Django random number
- Convert HTML page to PDF using Django
- How to get data from get request in Django
- Python Django get – Everything you need to know
- Outputting Python to html Django
- Compare two integers in Python Django
In this Django tutorial, we discussed How to set the time zone in python Django. Also, we discussed the following lists of topics:
- Python Django set timezone
- Python Django set timezone settings
- Python Django set timezone UTC
- Python Django set timezone to EST
- Python Django set timezone to IST
- Python Django set timezone for user
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.