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 Sarina
import re
def time_converter(time):
time_regex = re.compile('(?P\d+):(?P\d+) (?P(?:a|p)\.m\.)').match
match = time_regex(time)
hour = int(match.group('hours'))
minute = int(match.group('minutes'))
form = match.group('format')
if (hour < 12 and form == 'p.m.') or (time == '12:00 a.m.'):
result = "{:02}:{:02}".format((hour+12)%24, minute)
else:
result = "{:02}:{:02}".format(hour, minute)
return result
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!")
Dec. 15, 2019