Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Time Converter (24h to 12h) by rn16
import datetime as dt
def time_converter(time):
frmt = "%H:%M"
T = dt.datetime.strptime(time, frmt)
if T.hour > 12:
return f"{T.hour - 12}:{time.split(':')[1]} p.m."
elif T.hour == 12 or T.hour == 0:
return f"12:{time.split(':')[1]} " + ("a.m." if T.hour == 0 else "p.m.")
else:
return f"{T.hour}:{time.split(':')[1]} a.m."
if __name__ == '__main__':
print("Example:")
print(time_converter('12:30'))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert time_converter('12:30') == '12:30 p.m.'
assert time_converter('09:00') == '9:00 a.m.'
assert time_converter('23:15') == '11:15 p.m.'
print("Coding complete? Click 'Check' to earn cool rewards!")
May 19, 2020