Skip to content

Commit 92190ed

Browse files
time conversion program
1 parent 476f570 commit 92190ed

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function timeConversion(s) {
2+
const hoursMap = {
3+
'12': 12,
4+
'01': 13,
5+
'02': 14,
6+
'03': 15,
7+
'04': 16,
8+
'05': 17,
9+
'06': 18,
10+
'07': 19,
11+
'08': 20,
12+
'09': 21,
13+
'10': 22,
14+
'11': 23
15+
}
16+
const timeArr = s.split(":")
17+
let hour = timeArr[0], minute = timeArr[1], seconds = timeArr[2]
18+
if (s.indexOf('PM') != -1) { // PM case
19+
seconds = seconds.replace('PM', '')
20+
return `${hoursMap[hour]}:${minute}:${seconds}`
21+
}
22+
if (s.indexOf('AM') != -1) { // AM case
23+
hour = (hour === '12' || hour === 12) ? '00' : hour
24+
seconds = seconds.replace('AM', '')
25+
return `${hour}:${minute}:${seconds}`
26+
}
27+
return null
28+
}
29+
30+
console.log(timeConversion('12:40:22PM'))

0 commit comments

Comments
 (0)