Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Using total minutes count (0..1439) solution in Clear category for Time Converter (24h to 12h) by assargin
def time_converter(time):
"""
total_minutes: 0..1439
hour: total_minutes // 60, but if hour mod 12 == 0 than hour = 12
minutes: simple total_minutes mod 60
ampm: simple a.m. if total_minutes < 12*60 else p.m.
"""
total_minutes = int(time[0:2]) * 60 + int(time[3:5])
hours = total_minutes // 60
h = 12 if hours%12==0 else hours%12
m = total_minutes % 60
ampm = 'a.m.' if total_minutes<12*60 else 'p.m.'
return f'{h}:{m:02d} {ampm}'
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.'
assert time_converter('00:00') == '12:00 a.m.'
assert time_converter('12:00') == '12:00 p.m.'
print("Coding complete? Click 'Check' to earn cool rewards!")
Feb. 9, 2020