-
Notifications
You must be signed in to change notification settings - Fork 0
/
time conversion.py
31 lines (26 loc) · 922 Bytes
/
time conversion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def convert_to_24_hour(time_str):
# Extract the time components
time_components = time_str.split(":")
hour = int(time_components[0])
print(hour)
minute = int(time_components[1][:-2]) # Remove "AM" or "PM" part
print(minute)
period = time_components[1][-2:] # Get "AM" or "PM"
print(period)
# Convert to 24-hour format
if period == "AM":
if hour == 12:
hour = 0
elif period == "PM":
if hour != 12:
hour += 12
# Format the hour and minute as strings with leading zeros
hour_str = str(hour).zfill(2)
minute_str = str(minute).zfill(2)
# Construct the 24-hour time string
time_24_hour = f"{hour_str}:{minute_str}"
return time_24_hour
# Test the function
time_12_hour = input("Enter the time")
time_24_hour = convert_to_24_hour(time_12_hour)
print(f"The time {time_12_hour} in 24-hour format is: {time_24_hour}")