简体   繁体   中英

How to get the local timezone of the system from Django?

I am entering the time from a local machine at a timezone +5:30. My TIME_ZONE is set to UTC in the settings.py file. Now the user entering the data from the django admin is entering it in his own local time zone which could be anywhere in the world. What I am thinking about is how do I convert this time into UTC before storing it in the DB? I need to store all data in the DB in UTC only. Any ideas?

First of all, you should get timezone of the user or time difference.

Example by time difference

Firstly, on client side you must get time difference:

 //JavaScript d = new Date() d.getTimezoneOffset() // print -240 

Secondly, time with the different should be converted to timestamp and send to server side. On the serverside from timestamp you can get time that can be saved to database.

go to settings.py of your Django project

comment timezone settings and use TIME_ZONE = timezone.now()

from django.utils import timezone


TIME_ZONE = timezone.now()

# TIME_ZONE = 'UTC'
# USE_I18N = True
# USE_L10N = True
# USE_TZ = True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM