12 hour clock to 24 hour clock in python
a=''
def timeConversion(s):
if s[-2:] == "AM" :
if s[:2] == '12':
a = str('00' + s[2:8])
else:
a = s[:-2]
else:
if s[:2] == '12':
a = s[:-2]
else:
a = str(int(s[:2]) + 12) + s[2:8]
return a
s = '11:05:45AM'
result = timeConversion(s)
print(result)
0
8
def timeConversion(s):
suffix = s[-2:]
h_str = s[0:2]
if suffix == 'PM':
hh = int(h_str) % 12 + 12
else:
hh = int(h_str) % 12
return s.replace(h_str, str(hh).zfill(2), 1)[:-2]
s = '12:00:00AM'
print(timeConversion(s))
# output: 00:00:00
Thank you!
8
0
4.22
9
>>> from datetime import *
>>> m2 = '1:35 PM'
>>> m2 = datetime.strptime(m2, '%I:%M %p')
>>> print(m2)
1900-01-01 13:35:00
Thank you!
9
0
Are there any code examples left?
New code examples in category Python