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'
Advertisement
Answer
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