Back to blog
Feb 11, 2024
7 min read

Reformat Date

Convert a date from 'Day Month Year' format to 'YYYY-MM-DD' format.

Difficulty: Easy | Acceptance: 68.50% | Paid: No Topics: String

Given a date string in the format Day Month Year, where:

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

Convert the date string to the format YYYY-MM-DD.

Examples

Input: date = "20th Oct 2052"
Output: "2052-10-20"
Input: date = "6th Jun 1933"
Output: "1933-06-06"
Input: date = "26th May 1960"
Output: "1960-05-26"

Constraints

The given dates are valid dates in the Gregorian calendar.

String Manipulation

Intuition Split the input string by spaces, extract day by removing the suffix, find month index from an array, and format all components with leading zeros.

Steps

  • Split the date string by spaces to get day, month, and year parts
  • Remove the last two characters from day to get the numeric day
  • Find the month index (1-12) by searching in a months array
  • Format day and month with leading zeros if needed
  • Combine in YYYY-MM-DD format
python
class Solution:\n    def reformatDate(self, date: str) -> str:\n        months = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"]\n        parts = date.split()\n        day = parts[0][:-2]\n        month = str(months.index(parts[1]) + 1).zfill(2)\n        year = parts[2]\n        return f\"{year}-{month}-{day.zfill(2)}\"

Complexity

  • Time: O(1) - Fixed number of operations regardless of input size
  • Space: O(1) - Using fixed-size arrays and variables
  • Notes: Simple and readable, but linear search for month is slightly inefficient

HashMap Lookup

Intuition Use a hash map to store month abbreviations mapped to their numeric values for O(1) lookup time.

Steps

  • Create a hash map mapping month abbreviations to their two-digit string representations
  • Split the input string by spaces
  • Extract day by removing the suffix and pad with leading zero
  • Look up month value from the hash map
  • Combine year, month, and day in YYYY-MM-DD format
python
class Solution:\n    def reformatDate(self, date: str) -> str:\n        month_map = {\"Jan\": \"01\", \"Feb\": \"02\", \"Mar\": \"03\", \"Apr\": \"04\", \"May\": \"05\", \"Jun\": \"06\",\n                     \"Jul\": \"07\", \"Aug\": \"08\", \"Sep\": \"09\", \"Oct\": \"10\", \"Nov\": \"11\", \"Dec\": \"12\"}\n        parts = date.split()\n        day = parts[0][:-2].zfill(2)\n        month = month_map[parts[1]]\n        year = parts[2]\n        return f\"{year}-{month}-{day}\"

Complexity

  • Time: O(1) - Constant time operations with hash map lookup
  • Space: O(1) - Fixed-size hash map with 12 entries
  • Notes: More efficient month lookup than array search, at the cost of slightly more initialization code

Date Parsing

Intuition Leverage built-in date parsing libraries to handle the conversion, parsing the custom format and outputting in standard format.

Steps

  • Parse the input string to extract day, month, and year components
  • Convert month abbreviation to numeric value
  • Create a Date object with the parsed components
  • Format the Date object to YYYY-MM-DD string
python
from datetime import datetime\n\nclass Solution:\n    def reformatDate(self, date: str) -> str:\n        months = {\"Jan\": 1, \"Feb\": 2, \"Mar\": 3, \"Apr\": 4, \"May\": 5, \"Jun\": 6,\n                  \"Jul\": 7, \"Aug\": 8, \"Sep\": 9, \"Oct\": 10, \"Nov\": 11, \"Dec\": 12}\n        parts = date.split()\n        day = int(parts[0][:-2])\n        month = months[parts[1]]\n        year = int(parts[2])\n        dt = datetime(year, month, day)\n        return dt.strftime(\"%Y-%m-%d\")

Complexity

  • Time: O(1) - Constant time operations
  • Space: O(1) - Fixed space for date object and formatting
  • Notes: Leverages built-in date libraries which can handle edge cases automatically, but may be overkill for this simple transformation