Reputation: 433
I have a datetime object representing when a user logged, from this object I want to check whether the datetime object expresses a date in the range today -7, so if today is 2018-06-21 I would like to return ever date between today and 2018-06-14. I attach an example of my code (terribly wrong).
def joined_today(self, query):
filtered = []
today = datetime.today().date()
for x in query:
if x[1].date() = today -7:
filtered.append(x)
return filtered
Upvotes: 0
Views: 41
Reputation: 599450
A couple of things here. Firstly, you can do date calculations using datetime.timedelta
. And secondly, you don't want to check that the date is equal to "today - 7", you want to check it is greater than that and less than or equal to today. So:
seven_days_ago = today - timedelta(days=7)
for x in query:
if seven_days_ago < x[1].date() <= today:
...
Upvotes: 2