Python string to Django timezone (aware datetime)
Solution 1
I know this is old but maybe will be helpful since I got into this situation as well:
What about using make_aware() ?
from datetime import datetime
from django.utils.timezone import make_aware
date = '22-05-2018'
aware = make_aware(datetime.strptime(date, '%d-%m-%Y'))
This will use the currently active timezone (activated by timezone.activate
). If no timezone is activated explicitly, it would use the default timezone -- TIME_ZONE
specified in settings.py
.
Solution 2
You are comparing time-zone unaware Python Date
objects with the time-zone aware DateTimeField
fields in your database. It is probably more intuitive to use DateTime
objects - and these can be made time-zone aware easily as follows:
import datetime
import pytz
start_date = '15-01-2016'
end_date = '16-01-2016'
date_format = '%d-%m-%Y'
unaware_start_date = datetime.datetime.strptime(start_date, date_format)
aware_start_date = pytz.utc.localize(unaware_start_date)
unaware_end_date = datetime.datetime.strptime(end_date, date_format)
aware_end_date = pytz.utc.localize(unaware_end_date)
my_list = MyModel.objects.filter(created_at__range=(aware_start_date, aware_end_date))
This creates unaware_start_date
and unaware_end_date
DateTime
objects using strptime()
. It then uses pytz.utc.localize
to make the objects time-zone aware (you will need to replace utc
with your relevant time-zone).
You can then have time-zone aware DateTime
objects - aware_start_date
and aware_end_date
. Feeding these into your filter should yield the desired results.
Solution 3
from django.utils import timezone
timestamp_raw = timezone.now() #current time, or use whatever time you have
date_format = '%Y-%m-%d %H:%M:%S' #time format day-month-year hour:minutes:seconds
timestamp = timezone.datetime.strftime(timestamp_raw, date_format)
Or Using the new f-string formatter
f"{timezone:%Y-%m-%d %H:%M:%S %p}"
Related videos on Youtube
Comments
-
jarussi over 2 years
TL;DR;
How to convert
2016-01-01
to Djangotimezone
?Full version:
I receive a query string parameter from a form and I wanna get that string and use it as a datetime filter in Django. The problem is that when I convert the string to a datetime, it's not making an aware datetime and so I lose a few hours due to timezone different. Maybe I'm losing myself in the formatting, but I'm not being able to do it.
I have pytz, I have
USE_TZ = True
in my settings as well.example:
from datetime import date # Example from what I receive as GET querystring parameter start_date, end_date = '15-01-2016', '16-01-2016' DATE_FORMAT = '%Y-%m-%d' start_date = start_date.split('-') start_date = date(int(start_date[2]), int(start_date[1]), int(start_date[0])) sd_filter = start_date.strftime(DATE_FORMAT) end_date = end_date.split('-') end_date = date(int(end_date[2]), int(end_date[1]), int(end_date[0])) ed_filter = end_date.strftime(DATE_FORMAT) #query my_list = MyModel.objects.filter(created_at__range=(sd_filter, ed_filter))
the problem lies in the filter. I'm losing a few hours due to timezone from Django settings.
UPDATE: I don't need to convert a
datetime.now()
to my time. I need to convert a string to datetime.-
gtlambert over 8 yearsWhat field type are you using in your model? A
DateField()
? If so, you can just pass the twodate
objects (start_date
andend_date
) to yourcreated_at__range=()
filter. -
Lajos Arpad over 8 yearsPossible duplicate of Python Timezone conversion
-
jarussi over 8 years@lambo477 It's a
DateTimeField
and I tryed doing that .. but it's still missing a few hours from utc. -
jarussi over 8 years@LajosArpad I tried using what was said in the answers but it didn't work for me :(
-
-
gtlambert over 8 yearsIf you don't have the
pytz
module installed, runpip install pytz
from your command line -
Gustavo Oliveira over 2 yearsDoes this apply to a date ISO string?
-
salafi over 2 yearsYes, It does . I have also edited the answer to include example using the new f-string formatter.