← Go Back

How to Validate a Date

import datetime

# The date entered by the user.
date_str = input("Enter a date in dd/mm/yyyy format: ")
# Try to convert it to a Python date object.
date = datetime.datetime.strptime(date_str, "%d/%m/%Y").date()
except ValueError:
print("The date is invalid.")
print("The date is correct!")

On the strptime() standard function, see How to Convert a String to a Date.

dates datetime

🐍 You might also find interesting: