Skip to content

Commit e0de0be

Browse files
authored
Create 1507_Reformat_Date.md
1 parent eab0e47 commit e0de0be

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

1507_Reformat_Date.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## 1507. Reformat Date
2+
3+
```
4+
Given a date string in the form Day Month Year, where:
5+
6+
Day is in the set {"1st", "2nd", "3rd", "4th", ..., "30th", "31st"}.
7+
Month is in the set {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}.
8+
Year is in the range [1900, 2100].
9+
Convert the date string to the format YYYY-MM-DD, where:
10+
11+
YYYY denotes the 4 digit year.
12+
MM denotes the 2 digit month.
13+
DD denotes the 2 digit day.
14+
15+
16+
Example 1:
17+
18+
Input: date = "20th Oct 2052"
19+
Output: "2052-10-20"
20+
Example 2:
21+
22+
Input: date = "6th Jun 1933"
23+
Output: "1933-06-06"
24+
Example 3:
25+
26+
Input: date = "26th May 1960"
27+
Output: "1960-05-26"
28+
29+
30+
Constraints:
31+
32+
The given dates are guaranteed to be valid, so no error handling is necessary.
33+
```
34+
35+
36+
```python
37+
class Solution:
38+
def reformatDate(self, date: str) -> str:
39+
m_dic = {"Jan":'01', "Feb":'02', "Mar":'03', "Apr":'04', "May":'05', "Jun":'06', "Jul":'07', "Aug":'08', "Sep":'09', "Oct":'10', "Nov":'11', "Dec":'12'}
40+
41+
date_str = date.split()
42+
y = str(date_str[2])
43+
m = str(m_dic[date_str[1]])
44+
d = str(date_str[0][:-2])
45+
if len(d) == 1:
46+
d = '0' + d
47+
# print(y + '-' + m + '-' + d)
48+
return '{}-{}-{}'.format(y, m, d)
49+
```
50+
51+
52+
53+
```
54+
Runtime: 52 ms, faster than 6.19% of Python3 online submissions for Reformat Date.
55+
Memory Usage: 14.4 MB, less than 19.79% of Python3 online submissions for Reformat Date.
56+
```

0 commit comments

Comments
 (0)