hi i'm new in working with datetime
objects in django
all I know now is that instead of python's datetime.datetime.now()
we should use django's timezone.now()
, i've also set TIMEZONE
and USE_TZ=True
in settings.py
but my problem is now for converting these types of time. as far as I know, even if we use timezone.now()
for saving in database, django uses UTC time to store in DB. so I need a simple syntax for converting UTC time into my local time which is set in settings.py and vice versa to get local time from human and return local time.
i've also seen that django has some template tags to do that, but since i am doing this mostly for a REST API with django-rest
for an android app, i need to be able to do this in python syntax.
thanks everyone, I hope I could be clear at what I mean :)
Asked
Active
Viewed 2,758 times
0

Amin_mmz
- 394
- 4
- 14
2 Answers
1
In templates, Django will automatically convert your model dates (stored as UTC) to the current time zone. The current time zone is set by settings.TIMEZONE
unless you explicitly change it somewhere else. You don't even need to use special template tags. This will convert fine:
{{ MyModel.my_date }}
Outside of templates, there is a tool called localtime
that you can use to do the same conversion.
from django.utils.timezone import localtime
...
local_date = localtime(MyModel.my_date)
print( str(MyModel.my_date) ) # UTF time
print( str(local_date) ) # local time
The datetime returned by localtime
is time zone aware. If you ever need a time zone naive datetime, you can convert it like this:
my_date = localtime(MyModel.my_date).replace(tzinfo=None)

John Meinken
- 439
- 4
- 10
-
thanks, very helpful, but i also need to get local time from user and save its converted to UTC in db. what is the best way for that?! – Amin_mmz Dec 01 '16 at 21:36
-
To the best of my understanding, that conversion is automatic. If you're using a ModelForm to get user input, for example, Django will assume any datetime inputs are in the user's local time, and will convert to UTC when saving to the database. – John Meinken Dec 01 '16 at 21:55
-
Oh, and if you're asking how to get a user's real local time zone instead of using `settings.TIMEZONE`, that's not so easy to do. See the answers for [this question](http://stackoverflow.com/questions/10235956/django-1-4-how-to-automatically-get-users-timezone-from-client). – John Meinken Dec 04 '16 at 03:48
-
"In templates, Django will automatically convert your model dates (stored as UTC) to the user's time zone." I do not believe this statement to be correct. Can you cite from the documentation? – e4c5 Dec 05 '16 at 07:21
-
[Time zone aware output in templates](https://docs.djangoproject.com/en/1.10/topics/i18n/timezones/#time-zone-aware-output-in-templates) - Note, by "user's time zone" I was referring to what Django calls the "current time zone". As I mentioned in an above comment, Django doesn't have a way to automatically determine a user's actual time zone. I will correct the terminology in my answer. – John Meinken Dec 05 '16 at 14:40
0
If, in settings.py we have the following:
from pytz import timezone
LOCAL_TZ = pytz.timezone('CST6CDT') # asume that local timezone is central, but you can use whatever is accurate for your local
Now, you can use this to convert from utc time to local
import pytz
from django.conf import settings
def to_local_dttm(utc_dttm):
return utc_dttm.astimezone(settings.LOCAL_TZ)
def to_utc_dttm(local_dttm):
return local_dttm.astimezone(pytz.utc)

2ps
- 15,099
- 2
- 27
- 47