1a=''
2def timeConversion(s):
3 if s[-2:] == "AM" :
4 if s[:2] == '12':
5 a = str('00' + s[2:8])
6 else:
7 a = s[:-2]
8 else:
9 if s[:2] == '12':
10 a = s[:-2]
11 else:
12 a = str(int(s[:2]) + 12) + s[2:8]
13 return a
14
15
16s = '11:05:45AM'
17result = timeConversion(s)
18print(result)
1>>> from datetime import *
2>>> m2 = '1:35 PM'
3>>> m2 = datetime.strptime(m2, '%I:%M %p')
4>>> print(m2)
51900-01-01 13:35:00
1def timeConversion(s):
2 suffix = s[-2:]
3 h_str = s[0:2]
4
5 if suffix == 'PM':
6 hh = int(h_str) % 12 + 12
7 else:
8 hh = int(h_str) % 12
9 return s.replace(h_str, str(hh).zfill(2), 1)[:-2]
10
11
12s = '12:00:00AM'
13print(timeConversion(s))
14# output: 00:00:00