Skip to main content
Back to problems
#1507
Easy Algorithms

Reformat date

String
68.3% acceptance
Feb 25, 2026
504
441
Given a date string in the form 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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn reformat_date(date: String) -> String {
    let parts: Vec<&str> = date.split_whitespace().collect();
    let day_str = parts[0];
    let month_str = parts[1];
    let year_str = parts[2];

    // Extract day digits
    let day: u32 = day_str.chars().take_while(|c| c.is_ascii_digit())
      .collect::<String>().parse().unwrap();

    let month = match month_str {
      "Jan" => 1u32, "Feb" => 2, "Mar" => 3, "Apr" => 4,
      "May" => 5, "Jun" => 6, "Jul" => 7, "Aug" => 8,
      "Sep" => 9, "Oct" => 10, "Nov" => 11, "Dec" => 12,
      _ => 0,
    };

    format!("{}-{:02}-{:02}", year_str, month, day)
  }
}