Python programs for conversion between 12-hour and 24-hour time format have been shown here. The 12-hour time format and the 24-hour time format are two different ways of representing time.
The 12-hour time format is a system in which the day is divided into two 12-hour periods: AM / am (ante meridiem) and PM / pm (post meridiem). In ante meridiem, the hours from 1 to 12 are used to represent the time from midnight to noon, while in post meridiem the hours from 1 to 12 are used to represent the time from noon to midnight. On the other hand, the 24-hour time format is a system in which the day is divided into 24 hours. In this system, the hours from 0 to 23 are used to represent the time from midnight to midnight.
For example, 11:15 am in the 12-hour format is equivalent to 11:15 in the 24-hour format, while 11:15 pm in the 12-hour format is equivalent to 23:15 in the 24-hour format.
1. Program & Output for Conversion Between 12-Hour Time Format and 24-Hour Format
1.1. Python Program & Output to convert 12 hour time format to 24 hour format
#################################### # alphabetacoder.com # Python program to convert 12 hour # time format to 24 hour format #################################### def changeTimeFormat(time): # Split the input into hours, minutes, and am/pm parts p = time.split(":") # get hour h = int(p[0]) # get minute m = int((p[1].split(" "))[0]) # get am or pm am_pm = (p[1].split(" "))[1] # copy value h1 = h m1 = m # Adjust the hour if am_pm == "am" or am_pm == "AM": # if it is am / AM # if hour = 12, then it is 0 in 24 hour format if h1 == 12: h1 = 0 # display result print(f"{h}:{m} a.m is same as {h1:02d}:{m1:02d} in 24-hour format") elif am_pm == "pm" or am_pm == "PM": # if it is pm / PM # if hour not equals 12, then add 12 in 24 hour format if h1 != 12: h1 += 12 # display result print(f"{h}:{m} p.m is same as {h1:02d}:{m1:02d} in 24-hour format") else: print("Wrong Input Format!") def main(): # take inputs time = input("Enter the time in 12-hour format (hh:mm am/pm): ") # call function to display time format in 24 hour format changeTimeFormat(time) # driver code main()
Output
Enter the time in 12-hour format (hh:mm am/pm): 2:45 am
2:45 a.m is same as 02:45 in 24-hour format
1.2. Python Program & Output to convert 24 hour time format to 12 hour format
#################################### # alphabetacoder.com # Python program to convert 24 hour # time format to 12 hour format #################################### def changeTimeFormat(time): # Split the input into hours, minutes p = time.split(":") # get hour h = int(p[0]) # get minute m = int(p[1]) # copy value h1 = h m1 = m # Adjust the hour if h1 < 12: # if hour value less than 12 # display the result print(f"{h}:{m} is same as {h1:02d}:{m1:02d} a.m in 12-hour format") else: # if hour value greater or equal to 12 # subtract 12 from hour value h1 = h1 - 12 print(f"{h}:{m} is same as {h1:02d}:{m1:02d} p.m in 12-hour format") def main(): # take inputs time = input("Enter the time in 24-hour format (hh:mm): ") # call function to display time format in 12 hour format changeTimeFormat(time) # driver code main()
Output
Enter the time in 24-hour format (hh:mm): 20:15
20:15 is same as 08:15 p.m in 12-hour format