Reformat Date - Problem

Given a date string in the form Day Month Year, convert it to the format YYYY-MM-DD.

Input Format:

  • 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]

Output Format:

  • YYYY denotes the 4-digit year
  • MM denotes the 2-digit month (01-12)
  • DD denotes 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
Date Format ConversionInput"20th Oct 2052"Output"2052-10-20"20thOct2052Day: 20Month: 10Year: 2052Split → Convert → Format with zero padding
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
Asked in
Amazon 15 Microsoft 12 Facebook 8
25.4K Views
Medium Frequency
~10 min Avg. Time
856 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