Earlier we have seen the easiest way to convert date object to mm/dd/yyyy string format. Now we will see how to do the reverse of it. i.e. converting date string of mm/dd/yyyy or dd/mm/yyyy to date object. Here, I have used the datetime.strptime() method to convert the date string to datetime object. Then used the date() method to convert it to date object. Here is the example.
import datetime
## mm/dd/yyyy to date object
dateStr = "08/23/2015"
dateObj = datetime.datetime.strptime(dateStr, "%m/%d/%Y").date()
print("\n Date: ", dateObj, "\n")
print("\n Date: ", type(dateObj), "\n")
## dd/mm/yyyy to date object
dateStr = "18/02/2002"
dateObj = datetime.datetime.strptime(dateStr, "%d/%m/%Y").date()
print("\n Date: ", dateObj, "\n")
print("\n Date: ", type(dateObj), "\n")
Read my other article on converting date and time string to datetime object.
Reference
- About datetime.strptime() as Python Docs.