2018-04-22T10:28:39
Getting the Time Right in Django
If you've ever touched a date or time related field via the Django API, you'll have noticed the big warning that the date and/or time is not localized. While the warning is nice there isn't much more in the way of how to localize the time. Here are two ways that I usually go about it:
>>> timezone.make_aware()
>>> timezone.localtime()
Both are found in django.utils
's timezone module. If you're using Django Extensions, timezone
is already loaded when you run ./manage.py shell_plus
. If you're not, simply run from django.utils import timezone
.
For usage:
>>> help(timezone.make_aware)
>>> help(timezone.localtime)