I'm working in django, but standard python solution is ok too.
I'm converting a code which uses a naive-datetime to use aware-datetime.
Below is the original code:
today = datetime.today()
MyClass.objects.filter(datetimefield__range=(today, today+datetime.timedelta(1)) )
How do I convert it to use timezone-aware time?
If the time is jun/3rd/7:20pm locally,
I'd like to get datetime range of [jun/3rd/00:00am, jun/4th/00:00am]
(midnight to midnight which will include now
)
I know this is very old, but using django.utils.timezone this is fairly straightforward. (nothing concerning timezones ever seems easy to me)
# this is could be any datetime.date
my_date = timezone.now().date()
dt_at_local_midnight = timezone.make_aware(
timezone.datetime.combine(my_date, time.min),
timezone.get_current_timezone())
and alternative that might be better is listed in the first comment below.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With