-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1507_reformat_date.go
More file actions
53 lines (50 loc) · 912 Bytes
/
1507_reformat_date.go
File metadata and controls
53 lines (50 loc) · 912 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package leetcode
// https://leetcode.com/problems/reformat-date/
// For day, we can scan until facing a non number
// Then prefix it with 0 if needed
// For month and year the length is fixed
func reformatDate(date string) string {
day := ""
month := ""
year := ""
i := 0
for ; i < 4 && date[i] >= '0' && date[i] <= '9'; i++ {
day = day + string(date[i])
}
if len(day) == 1 {
day = "0" + day
}
i = i + 3
month = toMonth(date[i : i+3])
year = date[len(date)-4:]
return year + "-" + month + "-" + day
}
func toMonth(month string) string {
switch month {
case "Jan":
return "01"
case "Feb":
return "02"
case "Mar":
return "03"
case "Apr":
return "04"
case "May":
return "05"
case "Jun":
return "06"
case "Jul":
return "07"
case "Aug":
return "08"
case "Sep":
return "09"
case "Oct":
return "10"
case "Nov":
return "11"
case "Dec":
return "12"
}
return ""
}