Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
not the best with checks solution in Uncategorized category for Time Converter (12h to 24h) by akamus
from datetime import time as t
def time_converter(time):
if len(time) == 9:
time = "0" + time
hour = int(time[0:2])
minutes = int(time[3:5])
period = time[6:10]
if period == "p.m." and hour != 12:
hour += 12
if period == "a.m." and hour == 12:
hour = 0
return t(hour, minutes).strftime("%H:%M")
if __name__ == "__main__":
print("Example:")
print(time_converter("12:30 p.m."))
# These "asserts" using only for self-checking and not necessary for auto-testing
assert time_converter("12:30 p.m.") == "12:30"
assert time_converter("9:00 a.m.") == "09:00"
assert time_converter("11:15 p.m.") == "23:15"
print("Coding complete? Click 'Check' to earn cool rewards!")
Sept. 9, 2024