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 "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 length

Example: "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
Date Format Conversion ProcessInput: "20th Oct 2052"Human-friendly formatStep 1ParseStep 2CleanStep 3ConvertStep 4Format"20th""Oct""2052"Remove suffixHash lookupKeep as-is"20""10""2052"Zero-pad and combineOutput: "2052-10-20"ISO 8601 standard format
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.
Asked in
Google 45 Amazon 38 Meta 28 Microsoft 25
23.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen