Reformat Date — Solution in Python, Java & C++
Reformat Date solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on strings and simulation.
Reformat Date is an easy coding problem on strings and simulation, asked in Amazon and Bloomberg interviews.
- Difficulty: Easy
- Topics: Strings, Simulation
- Asked at: Amazon, Bloomberg
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
A date is written as "Day Month Year", where:
Dayis an ordinal such as"1st","2nd","3rd","4th", …,"30th","31st";Monthis one of"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec";Yearis a four-digit year between 1900 and 2100.
Convert it to the YYYY-MM-DD format: the four-digit year, the two-digit month and the two-digit day, joined by dashes. The given date is always a real calendar date.
Example 1
Input: date = "2nd Oct 2026"
Output: "2026-10-02"
Example 2
Input: date = "31st Dec 1999"
Output: "1999-12-31"
Example 3
Input: date = "13th Mar 2100"
Output: "2100-03-13"
Constraints
the given dates are guaranteed to be validthe year is in the range [1900, 2100]
Hints for Reformat Date
Hint 1
Split the date on spaces into its three parts.
Hint 2
The day is the number in front of its two-letter suffix (
st,nd,rdorth).Hint 3
Look the month up in a 12-entry table, then pad the month and day to two digits.
How to solve Reformat Date
The input is fully structured, so this is a parsing exercise: split into three tokens, strip the ordinal suffix from the day, map the month name to its number and print with zero-padding.
Approach
- Split
dateon spaces intoday,month,year. - Drop the last two characters of
dayand read the rest as a number. - Find
monthin the listJan … Dec; its 1-based position is the month number. - Return
year + "-" + pad2(month) + "-" + pad2(day).
Why it works
Every suffix is exactly two letters, so removing two characters leaves the digits whatever the number. The year is already four digits, and padding month and day to width two produces the required fixed-width format.
Complexity
- Time —
O(1) - Space —
O(1)
Pitfalls
- Days 1–9 and months 1–9 need a leading zero.
- Do not try to strip
"st"with a search —"1st"and"21st"both end in it, but cutting a fixed two characters is simpler and always right. - The month is a 1-based number; a 0-based table index must be shifted.
Reference solution
Python
def reformatDate(date: str) -> str:
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
d, m, y = date.split()
return "%s-%02d-%02d" % (y, months.index(m) + 1, int(d[:-2]))Java
public static String reformatDate(String date) {
String[] months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
String[] p = date.split(" ");
int day = Integer.parseInt(p[0].substring(0, p[0].length() - 2));
int mon = 0;
for (int i = 0; i < 12; i++) if (months[i].equals(p[1])) mon = i + 1;
return String.format("%s-%02d-%02d", p[2], mon, day);
}C++
string reformatDate(string date) {
vector<string> months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
istringstream in1(date);
string d, m, y;
in1 >> d >> m >> y;
int day = stoi(d.substr(0, d.size() - 2));
int mon = (int)(find(months.begin(), months.end(), m) - months.begin()) + 1;
char buf[16];
snprintf(buf, sizeof(buf), "%s-%02d-%02d", y.c_str(), mon, day);
return string(buf);
}JavaScript
var reformatDate = function(date) {
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var p = date.split(" ");
var day = parseInt(p[0].slice(0, p[0].length - 2), 10);
var mon = months.indexOf(p[1]) + 1;
return p[2] + "-" + (mon < 10 ? "0" : "") + mon + "-" + (day < 10 ? "0" : "") + day;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 515 strings problems · the whole catalogue
Learn the technique: Strings