Reformat Date - Problem
Given a date string in the form Day Month Year, convert it to the format YYYY-MM-DD.
Input Format:
Dayis in the set {"1st", "2nd", "3rd", "4th", ..., "30th", "31st"}Monthis in the set {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}Yearis in the range [1900, 2100]
Output Format:
YYYYdenotes the 4-digit yearMMdenotes the 2-digit month (01-12)DDdenotes the 2-digit day (01-31)
Input & Output
Example 1 — Basic Case
$
Input:
date = "20th Oct 2052"
›
Output:
"2052-10-20"
💡 Note:
Split into day=20th, month=Oct, year=2052. Remove 'th' to get day=20, Oct is month 10, format as 2052-10-20
Example 2 — Single Digit Day
$
Input:
date = "6th Jun 1933"
›
Output:
"1933-06-06"
💡 Note:
Day 6th becomes 06, Jun is month 06, year 1933. Result: 1933-06-06 with zero padding
Example 3 — End of Year
$
Input:
date = "31st Dec 2099"
›
Output:
"2099-12-31"
💡 Note:
Last day of year: 31st becomes 31, Dec is month 12, format as 2099-12-31
Constraints
- Date string is always valid and in the format "Day Month Year"
- Day is in the set {"1st", "2nd", "3rd", ..., "31st"}
- Month is in the set {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
- Year is in the range [1900, 2100]
Visualization
Tap to expand
Understanding the Visualization
1
Input
Human-readable date: "20th Oct 2052"
2
Process
Split components and convert: day=20, Oct→10, year=2052
3
Output
ISO format with padding: "2052-10-20"
Key Takeaway
🎯 Key Insight: Parse each date component separately and use hash map for efficient month name to number conversion
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code