There was an error while loading. Please reload this page.
1 parent 476f570 commit 92190edCopy full SHA for 92190ed
String-Questions/12hours-to-24hours.js
@@ -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