Skip to content

Commit 6756d84

Browse files
authored
Create 1507-reformat-date.js
1 parent b5acc53 commit 6756d84

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

1507-reformat-date.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @param {string} date
3+
* @return {string}
4+
*/
5+
const reformatDate = function(date) {
6+
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
7+
const map = new Map();
8+
for (let i = 0; i < months.length; ++i) {
9+
map.set(months[i], (i + 1 < 10 ? "0" : "") + (i+1));
10+
}
11+
const parts = date.split(" ");
12+
const day = (parts[0].length == 3 ? "0" : "") + parts[0].slice(0, parts[0].length - 2);
13+
return parts[2] + "-" + map.get(parts[1]) + "-" + day;
14+
};

0 commit comments

Comments
 (0)