Reputation: 541
I can't find any answer to this that works for me. I'm trying to check difference between datetime for created post with datetime.now()
example of what I would like to do in view.py
if (datetime.now() - post.created_at).minutes > 10:
Do_this
else:
Do_that
I have tried with timedelta things. Also tried strptime with datetime formats. No luck
When I print the datetime.now() i get: 2018-12-10 20:22:10.535052 And with the post.created_at: 2018-12-10 20:18:52:544396+00:00
How do I make them comparable?
Upvotes: 2
Views: 1371
Reputation: 48962
Django is giving you a timezone-aware datetime
object. datetime.now()
is giving you a timezone-naive datetime
object. You can't compare these.
Instead use django.utils.timezone.now()
, which provides the type of datetime
object that matches your Django settings (in your case, a timezone-aware datetime
).
Also note that timedelta
objects, as documented, don't have a minutes
attribute. They do have a seconds
attribute.
In summary:
if timezone.now() - post.created_at > timedelta(minutes=10):
Do_this
else:
Do_that
Upvotes: 2