189👍
Change the field TIME_ZONE
in the settings.py
.
For the Indian standard time you will need:
TIME_ZONE = 'Asia/Kolkata'
For more information about the TIME_ZONE in Django you can see: https://docs.djangoproject.com/en/dev/ref/settings/#time-zone
14👍
check django_timezones! this may help others too
it consists of all other timezones for references
- [Django]-How do I use an UpdateView to update a Django Model?
- [Django]-What's the difference between CharField and TextField in Django?
- [Django]-Django-DB-Migrations: cannot ALTER TABLE because it has pending trigger events
12👍
TIME_ZONE = 'Asia/Kolkata'
USE_I18N = True
USE_L10N = True
USE_TZ = True
The above settings should work
- [Django]-Django Admin: OneToOne Relation as an Inline?
- [Django]-CSRF Failed: CSRF token missing or incorrect
- [Django]-Setting the selected value on a Django forms.ChoiceField
6👍
Use Below settings its worked for me.
TIME_ZONE = 'Asia/Kolkata'
USE_I18N = True
USE_L10N = True
USE_TZ = False
- [Django]-Find Monday's date with Python
- [Django]-How do I display the Django '__all__' form errors in the template?
- [Django]-Django migration fails with "__fake__.DoesNotExist: Permission matching query does not exist."
4👍
In general, Universal Time(UTC) based on the mean sidereal time as measured in Greenwich, England. It’s also approximately equal to mean solar time from Greenwich. If Daylight Saving Time is in effect in the time zone, one must add 1 hour to the above standard times.
Django got all these timezone support, The django.utils timezone module, just returns datetime based on the USE TZ
setting, they are simply datetime objects with timezone ‘awareness’.
For IST(India Standard Time), which is UTC + 5:30, set TIME_ZONE = 'Asia/Kolkata'
and USE_TZ = True
, also USE_I18N = True, USE_L10N = True
, which are Internationalization and localization settings.
For a reference,
from django.utils import timezone
import datetime
print(timezone.now()) # The UTC time
print(timezone.localtime()) # timezone specified time,if timezone is UTC, it is same as above
print(datetime.datetime.now()) # default localmachine time
# output
2020-12-11 09:13:32.430605+00:00
2020-12-11 14:43:32.430605+05:30 # IST is UTC+5:30
2020-12-11 14:43:32.510659
refer timezone settings in django docs for more details.
- [Django]-Django ModelForm to have a hidden input
- [Django]-'staticfiles' is not a valid tag library: Template library staticfiles not found
- [Django]-Custom QuerySet and Manager without breaking DRY?
2👍
Adding to Jon Answer, If timezone.now()
still not working after changing the TIME_ZONE='Asia/Kolkata'
.
Instead of timezone.now()
you can use timezone.localtime()
.
Hope it solves.. 🙂
- [Django]-Django SUM Query?
- [Django]-Django model CharField: max_length does not work?
- [Django]-Django models: get list of id
2👍
All the solutions given above are working.
see the code i have used to get my timezone. my timezone is Indian standard time zone.
https://docs.djangoproject.com/en/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Asia/Kolkata'
USE_I18N = True
USE_L10N = True
USE_TZ = True
- [Django]-Django REST framework: non-model serializer
- [Django]-Django development server reload takes too long
- [Django]-List field in model?
2👍
Change your settings.py
to:
TIME_ZONE = 'Asia/Calcutta'
USE_I18N = True
USE_L10N = True
USE_TZ = False
This should work.
For more information about the TIME_ZONE in Django you can see: https://docs.djangoproject.com/en/dev/ref/settings/#time-zone
- [Django]-How can I use redis with Django?
- [Django]-Foreign Key Django Model
- [Django]-Django-Admin: CharField as TextArea
1👍
Django stores timestamp so if we just change the TIME_ZONE
variable Django will handle rest.
TIME_ZONE = 'Asia/Kolkata'
- [Django]-Composite primary key in django
- [Django]-IntegrityError duplicate key value violates unique constraint – django/postgres
- [Django]-In a django model custom save() method, how should you identify a new object?
- [Django]-Proper way to handle multiple forms on one page in Django
- [Django]-With DEBUG=False, how can I log django exceptions to a log file
- [Django]-Can I have a Django model that has a foreign key reference to itself?
0👍
Keep TIME_ZONE = ‘Asia/Kolkata’ in settings.py file and restart the service from where you are accessing the timezone (server or shell).
In my case, I restarted the python shell in which I was working and it worked fine for me.
- [Django]-Change a field in a Django REST Framework ModelSerializer based on the request type?
- [Django]-How can I test binary file uploading with django-rest-framework's test client?
- [Django]-Token Authentication for RESTful API: should the token be periodically changed?
0👍
Simple Change TIME ZONE from ‘UTC’ to ‘Asia/Kolkata’ remember K and A is Capital here.
- [Django]-Backwards migration with Django South
- [Django]-Check if celery beat is up and running
- [Django]-Django Rest Framework partial update
0👍
LANGUAGE_CODE = ‘en-us’
TIME_ZONE = ‘Asia/Calcutta’
USE_I18N = True
USE_L10N = True
USE_TZ = True
This should work.
- [Django]-How do I add a custom column with a hyperlink in the django admin interface?
- [Django]-Get current user in Model Serializer
- [Django]-Modulus % in Django template
0👍
for India user time below few lines of code in settings.py file
settings.py
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Asia/Kolkata'
USE_I18N = True
USE_L10N = True
USE_TZ = False
Don’t forget False in USE_TZ
- [Django]-Django template tag to truncate text
- [Django]-Django REST Framework serializer field required=false
- [Django]-Django can' t load Module 'debug_toolbar': No module named 'debug_toolbar'
0👍
First of all use these settings:
TIME_ZONE = 'Asia/Kolkata'
USE_I18N = True
USE_L10N = True
USE_TZ = True
Confirm its working on your local time using shell. Log into your shell & try this
from datetime import datetime
print(str(datetime.now())) # '2023-06-14 12:11:50.923150'
or
now = f"{datetime.now()}"
print(now) # '2023-06-14 12:11:50.923150'
print the string representation of datetime rather than just
print(datetime.now())
and compare with your local time.
- [Django]-Django: Equivalent of "select [column name] from [tablename]"
- [Django]-How to require login for Django Generic Views?
- [Django]-How do you get PyPy, Django and PostgreSQL to work together?
-1👍
change TIME_ZONE in settings.py.
TIME_ZONE = 'Asia/Colombo' # 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
to check it is working properly.
python manage.py shell
from django.utils import timezone
timezone.localtime(timezone.now())
then just check your time 🙂
here is List of tz database time zones as per wiki.
- [Django]-Django Model Fields Indexing
- [Django]-Django request get parameters
- [Django]-How can I handle Exceptions raised by dango-social-auth?