Convert string to date Python

In this Python string tutorial, you will learn how to convert string to date in Python.

# Import datetime module
import datetime

# Create a string
S = "2021-09-20"

# Convert string into date
D = datetime.datetime.strptime(S, "%Y-%m-%d").date()

# Print date
print(D)

# Display the type of D
print(type(D))

Output:

2021-09-20
<class 'datetime.date'>

Free learning resources: AiHintsCodeAllow

Leave a Comment

Your email address will not be published.