Reputation: 47
I have a date stored in a text file in this format "19 May 2021"
, I want to compare this date to the today's date to see if we have reached this date or not (> / <) comparisons. I was instructed to first convert this string date to a date object, I am failing to do so.
For overdue_tasks in task_overview:
# Iterating through the file
from datetime import date as dt
overdue_tasks = overdue_tasks.split(", ")
dates = overdue_tasks[4] # This date is found in this index
date_obj = dt.strptime(dates, "%d %b %Y")
print(date_obj) # Trying to see if the date did convert
This is the error I get:
line 27, in <module>
date_obj = dt.strptime(dates, "%m %d %y")
AttributeError:
type object 'datetime.date' has no attribute 'strptime'
Upvotes: 2
Views: 142
Reputation: 344
from datetime import datetime as dt
_your_date = "19 May 2021"
# your date
date_obj = dt.strptime(_your_date, "%d %b %Y")
now_date_obj = dt.now()
if date_obj < now_date_obj:
#TO DO
print('reached')
else:
#TO DO
print('not reached')
Upvotes: 1
Reputation: 27577
The strptime()
method is from datetime.datetime
, not datetime.time
. See the documentation for datetime.datetime.strptime()
.
So you'll simply need to change
from datetime import date as dt
to
from datetime import datetime as dt
Upvotes: 2