How to determine if the string is valid There are certain criteria that the date in the slashes format must fit to be valid: The string must separate the day, month and year with slashes. This criteria makes the strings "03142022" or "10,24,2025" invalid date strings. Day, month, and year must be composed of digits only. This makes the string "Jan/12/2022" invalid. Note: .isdigit() function is perfect for checking if a string is numeric (if it can be converted to an integer) The month must be within 1 and 12. This criteria makes the string "13/28/2025" invalid. The day must be within the max days limit for a given month. For instance, there are only 30 days on November, but 31 days in December. Therefore, "11/31/2022" is invalid while "12/31/2022" is valid. You will have a dictionary for how many days each month has. For simplicity, let's assume that February always has 28 days. Requirements for the program Write a function validate_date(date_string) which takes in a datestring and checks if the datestring is valid. If the string is valid, we want to return a tuple containing True and the date_string itself. If the string is invalid for a specific reason, we want to return a tuple containing False and the error code explaining what was wrong with the date string passed in.
How to determine if the string is valid
There are certain criteria that the date in the slashes format must fit to be valid:
-
The string must separate the day, month and year with slashes. This criteria makes the strings "03142022" or "10,24,2025" invalid date strings.
-
Day, month, and year must be composed of digits only. This makes the string "Jan/12/2022" invalid. Note: .isdigit() function is perfect for checking if a string is numeric (if it can be converted to an integer)
-
The month must be within 1 and 12. This criteria makes the string "13/28/2025" invalid.
-
The day must be within the max days limit for a given month. For instance, there are only 30 days on November, but 31 days in December. Therefore, "11/31/2022" is invalid while "12/31/2022" is valid. You will have a dictionary for how many days each month has. For simplicity, let's assume that February always has 28 days.
Requirements for the program
- Write a function validate_date(date_string) which takes in a datestring and checks if the datestring is valid. If the string is valid, we want to return a tuple containing True and the date_string itself. If the string is invalid for a specific reason, we want to return a tuple containing False and the error code explaining what was wrong with the date string passed in.
Step by step
Solved in 2 steps with 1 images