Reformat Date - Problem
Date String Reformatter
You're working on a legacy data migration project where you need to convert human-readable dates into standardized format for database storage.
Given a date string in the friendly format
Input Format:
• Day: Ordinal numbers like
• Month: Three-letter abbreviations
• Year: Four-digit year between 1900 and 2100
Output Format:
•
Example:
You're working on a legacy data migration project where you need to convert human-readable dates into standardized format for database storage.
Given a date string in the friendly format
"Day Month Year", convert it to the ISO standard format "YYYY-MM-DD".Input Format:
• Day: Ordinal numbers like
"1st", "2nd", "3rd", "4th", ..., "30th", "31st"• Month: Three-letter abbreviations
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"• Year: Four-digit year between 1900 and 2100
Output Format:
•
YYYY-MM-DD where each component is zero-padded to required lengthExample:
"20th Oct 2052" → "2052-10-20" Input & Output
example_1.py — Standard Date
$
Input:
date = "20th Oct 2052"
›
Output:
"2052-10-20"
💡 Note:
Day '20th' becomes '20' (remove 'th' suffix), 'Oct' maps to '10', year '2052' stays the same. Format as YYYY-MM-DD with zero-padding.
example_2.py — Single Digit Day
$
Input:
date = "6th Jun 1933"
›
Output:
"1933-06-06"
💡 Note:
Day '6th' becomes '6', then zero-padded to '06'. 'Jun' maps to '06', resulting in '1933-06-06'.
example_3.py — Different Ordinal Suffix
$
Input:
date = "22nd May 2025"
›
Output:
"2025-05-22"
💡 Note:
Day '22nd' becomes '22' (remove 'nd' suffix), 'May' maps to '05'. All ordinal suffixes (st, nd, rd, th) are handled the same way.
Constraints
- The given dates are guaranteed to be valid
- Day is in the set {"1st", "2nd", "3rd", "4th", ..., "30th", "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]
- Input format is always "Day Month Year" with spaces as separators
Visualization
Tap to expand
Understanding the Visualization
1
Parse Components
Split the friendly date into its three parts: day, month, year
2
Clean Day
Remove the ordinal suffix (st/nd/rd/th) to get the numeric day
3
Convert Month
Use hash map lookup to convert 3-letter month to 2-digit number
4
Format Output
Apply zero-padding and combine in YYYY-MM-DD format
Key Takeaway
🎯 Key Insight: Hash maps provide O(1) lookup time, making month conversion instant and eliminating the need for lengthy conditional chains.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code