Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Time Converter (12h to 24h) by keromage
def time_converter(text):
hours, minutes = text.split(':')
minutes, am_pm = minutes.split(' ')
hours = int(hours)
minutes = int(minutes)
if am_pm == "p.m." and hours != 12:
hours += 12
if am_pm == "a.m." and hours == 12:
hours -= 12
h = (hours <10)*"0" + str(hours)
m = (minutes<10)*"0" + str(minutes)
return 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!")
Oct. 1, 2019
Comments: