python - Convert 12 hour into 24 hour times -


i'm trying convert times 12 hour times 24 hour times...

automatic example times:

06:35  ## morning 11:35  ## morning (if m2 anywhere between 10:00 , 12:00 (morning mid-day) during times of 10:00 , 13:00 (1pm) m2 time morning time) 1:35  ## afternoon 11:35  ## afternoon 

example code:

m2 = "1:35" ## in afternoon. m2 = datetime.strptime(m2, "%h:%m") print m2 

expected output:

13:35 

actual output:

1900-01-01 01:35:00 

i tried second variation again didn't :/

m2 = "1:35" ## in afternoon. m2split = m2.split(":") if len(m2split[0]) == 1:     m2 = ("""%s%s%s%s""" % ("0", m2split[0], ":", m2split[1]))     print m2 m2temp = datetime.strptime(m2, "%i:%m") m2 = m2temp.strftime("%h:%m") 

what doing wrong , how can fix this?

try :)

code:

currenttime = datetime.datetime.now().time().strftime("%h:%m") if currenttime >= "10:00" , currenttime <= "13:00":     if m2 >= "10:00" , 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 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -