Convert 12 hour into 24 hour times
This approach uses strptime and strftime with format directives as per https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior, %H is the 24 hour clock, %I is the 12 hour clock and when using the 12 hour clock, %p qualifies if it is AM or PM.
>>> from datetime import datetime
>>> m2 = '1:35 PM'
>>> in_time = datetime.strptime(m2, "%I:%M %p")
>>> out_time = datetime.strftime(in_time, "%H:%M")
>>> print(out_time)
13:35
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
Try this :)
Code:
currenttime = datetime.datetime.now().time().strftime("%H:%M")
if currenttime >= "10:00" and currenttime <= "13:00":
if m2 >= "10:00" and m2 >= "12:00":
m2 = ("""%s%s""" % (m2, " AM"))
else:
m2 = ("""%s%s""" % (m2, " PM"))
else:
m2 = ("""%s%s""" % (m2, " PM"))
m2 = datetime.datetime.strptime(m2, '%I:%M %p')
m2 = m2.strftime("%H:%M %p")
m2 = m2[:-3]
print m2
Output:
13:35