Skip to content

Instantly share code, notes, and snippets.

@cansadadeserfeliz
Last active April 13, 2020 11:00
Show Gist options
  • Save cansadadeserfeliz/3e5107b11d19ac62a2ed to your computer and use it in GitHub Desktop.
Save cansadadeserfeliz/3e5107b11d19ac62a2ed to your computer and use it in GitHub Desktop.
Django: filter DatetimeField by today (max and min time)
# source: http://stackoverflow.com/questions/1317714/how-can-i-filter-a-date-of-a-datetimefield-in-django
from django.utils import timezone
today_min = datetime.datetime.combine(timezone.now().date(), datetime.time.min)
today_max = datetime.datetime.combine(timezone.now().date(), datetime.time.max)
MyModel.objects.get(date__range=(today_min, today_max))
@diptangsu
Copy link

This piece of code wasn't working for me today. So I forked this and made a few changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment