Find out if a date is more than 30 days old
you need to do something on similar lines:
from datetime import datetime, timedelta
time_between_insertion = datetime.now() - insertion_date
if time_between_insertion.days>30:
print "The insertion date is older than 30 days"
else:
print "The insertion date is not older than 30 days"
from datetime import datetime, timedelta
print(datetime.now())
datetime.datetime(2017, 9, 5, 7, 25, 37, 836117)
print(datetime.now() - timedelta(days=30))
datetime.datetime(2017, 8, 6, 7, 25, 51, 723674)
As you can see here, it is accurate down to seconds.
The problem is in datetime.today()
. You should use datetime.now()
instead of datetime.today()
:
time_since_insertion = datetime.now() - insertion_date
if time_since_insertion.days > 30:
print("The insertion date is older than 30 days")
else:
print("The insertion date is not older than 30 days")
Hope it helps!