12 hour clock to 24 hour clock in python

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
8
Nikolas 105 points

                                    def timeConversion(s):
    suffix = s[-2:]
    h_str = s[0:2]

    if suffix == 'PM':
        hh = int(h_str) % 12 + 12
    else:
        hh = int(h_str) % 12
    return s.replace(h_str, str(hh).zfill(2), 1)[:-2]


s = '12:00:00AM'
print(timeConversion(s))
# output: 00:00:00

0
0
4.22
9
Zugeni 75 points

                                    >>> from datetime import *
>>> m2 = '1:35 PM'
>>> m2 = datetime.strptime(m2, '%I:%M %p')
>>> print(m2)
1900-01-01 13:35:00

4.22 (9 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
convert 12 hour clock to 24 hour python python 12 hour to 24 hour how to convert 12 hors format to 24 hours in python python time format 12 hour am pm python time format 12 hour convert 12 hour to 24 hou python convert 12 hrs to 24 hrs in python how to convert 12hr to 24hr in python take input in python in form of hr min am/pm convert time in 24 hour format in pythom how to work out time right now python 24 hour clock how to convert a am pm string to 24 hrs time python converting str to 24 time only python converting str to 24 time pythin convert str into 24 hr time pythin convert into 24 hr time pythin 12 hour format to 24 hour format in python how to get time in military py converting to military time in python 12 hour clock to 24 hour clock in python python convert 12 hour time to 24 hour how to convert 24 hours to minutes in dataframe python datetime to 24 hr time python can we convert am to pm in python convert 12 hour time to 24 hour python python convert time to 24 hour time conversion in python how to convert into 24 hour format in python given a time in 12-hour am/pm format convert it to military (24-hour) time. in python Python 24 hour to am pm convert 12 hour clock to 24 hour clock python 24ours date time convert into am pm python conver 12 hour time to 24 python convert am/pm to 24 hour in python convert time from 24 hour clock to 12 hour clock format python change integer to 24 hour time python cahnge 24 hour time python time compare 12- and 24-hour time in python python convert datetime am pm to 24 hour HOW TO CONVERT AM PM IN PYTHON turning 24 hour time to int in python turning 24 hr time to int in python convert given data points in military time to 24 hours format in python convert 12 hour to 24 hour decimal in python convert 12 hour to 24 hour in python 12 hour format python code conversion 24 hours clock to 12 hour in python convert (12) to 12 in python 12hr to 24hr python convert AM to 24hr python time 00-12 format of hour in python convert railway time to normal time python python convert 12 hr clock to 24 hr convert 12 hour datetime to 24 hour datetime python SQLALCHMY 12 HOURS CONVERT TO 24 HOURS sqlalchemy convert date time formT IN 24 HOURS changing the time from 12 hour into 24 hour in pandas convert 12 hour format to 24 hour format in python python code for 24 hpur format to 12 hour format datetime python 12hr to 24hr convert from 12 hours to 24 python convert 24 hr to 12 hr python changing time from 12 hour to 24 hour representation pandas how to convert 12 hours to 24 hours in python convert 24 hour time to am pm python convert a string to time python 24 hour format convert 12hr to 24hr in python python 24 hour function convert datetime 12 to 24 hour time python convert 12 to 24 hour time python convert 12 hr to 24 hr in python convert 12h to 24h python convert am pm to 24 hour python python code for 24 hours format railway time converter python how to convert time format in python how to convert am pm to 24 hours python python program to convert time from 12 hour to 24 hour format python convert 12 hour to 24 hour lambda convert to 24 hour format in python convert date string to 24 hour time python Built in function for time conversion from AM/Pm into 24 hour format in python convert 12 hour to 24 hour python convert 12 hr to 24 hr python 12 hr to 24 hour time conversion python 12 hour to 24 hour python
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source