Time Conversion from 12-hour to 24-hour format in Python

In this tutorial, given time in 12-hour format as input we will learn how to perform time conversion into the 24-hour format and output it using Python.

 

#Taking 12h clock time value as a string input

ip = input("Enter Time in 12 Hour Format (HH:MM:SS AM/PM) : ")

#Checking if the time is AM and if it is 12AM. If yes, we change the 12 to 00

if("AM" in ip and ip[:2] == "12"):
    op = ("00" + ip[2:-2])
    
#Checking if the time is AM. Removing the 'AM' part in the output 24h clock value.
    
elif("AM" in ip):
    op = ip[2:-2]
    
#Checking if 'PM' is present and if it is 12PM. If yes, simply remove the 'PM' part in the output.
    
if("PM" in ip and ip[:2] == "12"):
    op = ip[:-2]

#Adding 12 to the output value in case it is any other 'PM' value other than '12PM'
    
else:
    op = str(int(ip[:2]) + 12) + ip[2:-2]
    
#Printing the output in 24-hour format    

print("24 Hour Format : " + op)

We use the ‘string slicing’ operation in a clever way in this algorithm.

First, we take the 12h clock value as an input string. Taking the input as string is important because we will later slice this string.

We then check if the provided 12h clock value is ’12 AM’. If yes, we change the ’12’ to ’00’. This is because in the 24h format, 12 AM means the start of a new day (new 24 hours).

If it is not ’12 AM’, we can simply write the same time and remove the ‘PM’ part.

We next check if the input time is PM. If yes, we need to add 12 to the hour part and remove the ‘PM’ to convert to 24h format.

However, there is an exception. If the time is ’12 PM’, we do not need to add 12 to the hour part. So, we simply remove the PM part and we get the required 24h format value.

Note:

This is a simple, yet very interesting way you can use the ‘slicing’ operation on a string.

You can learn more about fucntions about time from the link below :-

Time module in Python

Time Conversion in Python

Given time in 12-hour AM/PM format, we aim to convert it into the 24-hour(military) format.

Example:

10:40:06AM
10:40:06

10:40:06PM
22:40:06

Also, be mindful of the fact that midnight is at 12:00: 00 AM in 12-hour format and 00:00:00 in 24-hour format, and noon is at 12:00: 00 PM in 12-hour format and 12:00:00 in 24-hour format.

Implementation in Python

First, let us take the input.

time_12 = input().strip()

We use the strip() method to strip off any trailing and leading white spaces.

Let us now check if the given time is in AM or PM.

time_in_pm = time_12[-2:].lower() == 'pm'

If it is in AM then the time_in_pm variable will be holding False and if it is in PM then the time_in_pm variable will be holding True.

time_list = list(map(int, time_12[:-2].split(':')))

Here, we get the hours, minutes and seconds in the given time leaving the AM/PM part of the string.

If the time is in AM and the hours part is 12 then we need to change the hours part to 0. If the hours part is not equal to 12 then we can print the time without any changes.

if not time_in_pm and time_list[0] == 12:
    time_list[0] = 0

If the time is in PM then we need to add 12 to the hours part to convert it into the 24-hour format. If the hours part is equal to 12 then we need not change anything.

if time_in_pm and time_list[0] < 12:
    time_list[0] += 12

Let us now print the time in the 24-hour format.

print(':'.join(map(lambda x: str(x).rjust(2, '0'), time_list)))

Output:

06:30:21AM
06:30:21
06:30:21PM
18:30:21

Also read, Print time in all time zones in Python

Leave a Reply

Your email address will not be published. Required fields are marked *