Age Calculator with Validation - Problem
You need to calculate a person's age in years based on their birthdate and handle various error scenarios.
Input: A birthdate string in format "YYYY-MM-DD"
Output: Return the age as an integer, or an error message as a string
Validation Rules:
- Invalid date format should return
"Invalid date format" - Future dates should return
"Future date not allowed" - Impossible dates (like February 30th) should return
"Invalid date" - Valid dates should return the age in years
Note: Use today's date as "2024-01-15" for consistent testing.
Input & Output
Example 1 — Valid Birth Date
$
Input:
birthdate = "1990-03-15"
›
Output:
34
💡 Note:
Valid date format, not in future. Age = 2024 - 1990 = 34 (birthday has passed on March 15, 2024 > current Jan 15, 2024 is incorrect - birthday hasn't passed). Actually: 33 years old since March 15, 2024 hasn't occurred yet from Jan 15, 2024.
Example 2 — Invalid Date Format
$
Input:
birthdate = "1990/03/15"
›
Output:
"Invalid date format"
💡 Note:
Wrong separator - uses '/' instead of '-', format should be YYYY-MM-DD
Example 3 — Future Date
$
Input:
birthdate = "2025-01-01"
›
Output:
"Future date not allowed"
💡 Note:
Date is in the future compared to current date 2024-01-15
Constraints
- Input is a string representing a birthdate
- Expected format: YYYY-MM-DD
- Current date for testing: 2024-01-15
- Age calculated in complete years
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code