2021年1月29日星期五

Convert military time to regular time in python

I need to write a Python file that converts military time to regular time. In military time, hours are numbered from 00 to 23, like midnight is 00, 1 a.m. is 01, 1 p.m. is 13. time in hours and minutes is given as a 4 digit string with minutes following hours and given by 2 digits from 00 to 59. For example, military time 0022 = regular time 12:22 am.

I tried this:

time = eval(input('Enter military time: '))    def time():      return time[:2] + ':' + time[2:]  

but it does not work with input starts with 0 and i dunno how to convert the input as 'regular time' - 00=12:00

Would like to know what kind of approach should I take.

https://stackoverflow.com/questions/65963672/convert-military-time-to-regular-time-in-python January 30, 2021 at 09:07AM

没有评论:

发表评论