Convert 12 hour into 24 hour times

后端 未结 12 753
走了就别回头了
走了就别回头了 2020-12-31 08:17

I\'m trying to convert times from 12 hour times into 24 hour times...

Automatic Example times:

06:35  ## Morning
11:35  ## Morning (If m2 is anywhe         


        
12条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-31 08:24

    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)
    

提交回复
热议问题