Dates and times · 12 / 15 Exercise Medium

Dates with a custom format

Objective

Create a Python program that parses dates written as day/month/year and converts them to the standard ISO format, detecting the invalid ones. The strptime method does the opposite conversion of strftime and raises ValueError when the text does not match.

Instructions

  • Read an integer n and then n texts, one per line (no spaces).
  • Try to convert each text with datetime.strptime(text, "%d/%m/%Y"). If it works, show the date with isoformat() (YYYY-MM-DD).
  • If it raises ValueError (wrong format, or an impossible date such as 31 April), show Invalid date: text with the original text.
Input
4
15/03/2026
31/04/2026
2026-03-15
29/02/2024
Output
2026-03-15
Invalid date: 31/04/2026
Invalid date: 2026-03-15
2024-02-29

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise