Convert 12 hour into 24 hour times

后端 未结 12 672
走了就别回头了
走了就别回头了 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:18
    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)
    
    0 讨论(0)
  • 2020-12-31 08:18

    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

    0 讨论(0)
  • 2020-12-31 08:21

    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
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-12-31 08:26

    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")
    
    0 讨论(0)
  • 2020-12-31 08:27

    Try this

    0 讨论(0)
提交回复
热议问题