How can I get the timezone aware date in django?
It's not clear whether you're trying to end up with a date
object or a datetime
object, as Python doesn't have the concept of a "timezone aware date".
To get a date
object corresponding to the current time in the current time zone, you'd use:
# All versions of Djangofrom django.utils.timezone import localtime, nowlocaltime(now()).date()# Django 1.11 and higherfrom django.utils.timezone import localdatelocaldate()
That is: you're getting the current timezone-aware datetime
in UTC; you're converting it to the local time zone (i.e. TIME_ZONE
); and then taking the date from that.
If you want to get a datetime
object corresponding to 00:00:00 on the current date in the current time zone, you'd use:
# All versions of Djangolocaltime(now()).replace(hour=0, minute=0, second=0, microsecond=0)# Django 1.11 and higherlocaltime().replace(hour=0, minute=0, second=0, microsecond=0)
Based on this and your other question, I think you're getting confused by the Delorean package. I suggest sticking with Django's and Python's datetime functionality.