I\'m trying to convert times from 12 hour times into 24 hour times...
06:35 ## Morning
11:35 ## Morning (If m2 is anywhe
time = raw_input().strip() # input format in hh:mm:ssAM/PM
t_splt = time.split(':')
if t_splt[2][2:] == 'PM' and t_splt[0] != '12':
t_splt[0] = str(12+ int(t_splt[0]))
elif int(t_splt[0])==12 and t_splt[2][2:] == 'AM':
t_splt[0] = '00'
t_splt[2] = t_splt[2][:2]
print ':'.join(t_splt)
Instead of using "H" for hour indication use "I" as described in the example bellow:
from datetime import *
m2 = 'Dec 14 2018 1:07PM'
m2 = datetime.strptime(m2, '%b %d %Y %I:%M%p')
print(m2)
Please check this link
You need to specify that you mean PM instead of AM.
>>> from datetime import *
>>> m2 = '1:35 PM'
>>> m2 = datetime.strptime(m2, '%I:%M %p')
>>> print(m2)
1900-01-01 13:35:00
If date is in this format (HH:MM:SSPM/AM) the following code is effective :
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)
the most basic way is:
t_from_str_12h = datetime.datetime.strptime(s, "%I:%M:%S%p")
str_24h = t_from_str.strftime("%H:%M:%S")
Try this