# normal to military time conversion

unknown
python
2 years ago
5.1 kB
1
Indexable
Never
```# convert 12hr time into 24hr time(Normal time to Military Time)
'''
Algorithm:
1. Take input and check if time contains AM or PM
2. If time is in AM
i. Slice the string and remove AM
ii. Split the string by ':' and add to a list
iii. Create an empty list to store the military time
iv. If hours in normal time is 12 then it will be 00 hrs in military time
v. Rest hours, minutes and seconds are same in both normal and military time
vi. Join back the list and print military time
3. If time is in PM
i. Slice the string and remove AM
ii. Split the string by ':' and add to a list
iii. Create an empty list to store the military time
iv. If hours in normal time is 12 then it will be same in military time
v. Create dictionary with normal hours as key and military hours as value and add to the military list
vi. Join back the list and print military time
'''

def timeConversion(s):
# check if time is in am or pm

# if time is in am then
if 'AM' in user_input_time:
# slice the string and remove AM
cutted_time = user_input_time[:-2]
# then split the string by : and create a list
normal_splitted_time = list(cutted_time.split(':'))
# printed for checking
# print('Normal splitted time: ', normal_splitted_time)

# created empty list for printing military time
military_splitted_time = []
# used hrs for using index as 0 and so on
hrs = 0
mins = 1
secs = 2

# if hrs of normal time are 12
if normal_splitted_time[hrs] == '12':
# then set hrs of military time as 00
military_splitted_time.insert(hrs, '00')
else:
# set hrs of military time same as normal hrs
military_splitted_time.insert(hrs, (normal_splitted_time[hrs]))

# set minutes and seconds of military time same as normal time
military_splitted_time.insert(mins, (normal_splitted_time[mins]))
military_splitted_time.insert(secs, (normal_splitted_time[secs]))

# printed for cheching military time
# print('military splitted time', military_splitted_time)

# used string join method to join strings with colon
colon = ':'
military_time = colon.join(military_splitted_time)

print('Military time: ', military_time)

# if time in pm then
elif 'PM' in user_input_time:
# slice the string and remove PM
cutted_time = user_input_time[:-2]
# then split the string by :
normal_splitted_time = list(cutted_time.split(':'))
# printed for checking
# print('Normal splitted time: ', normal_splitted_time)

# created empty list for printing military time
military_splitted_time = []
# used hrs for using index as 0 and so on
hrs = 0
mins = 1
secs = 2

# created empty list
normal_hr_list = []
military_hr_list = []

# if hrs of normal time are 12
if normal_splitted_time[hrs] == '12':
# then set hrs of military time as 12
military_splitted_time.insert(hrs, '12')
else:
for normal_hr in range(1, 12):
if normal_hr < 10:
normal_hr_list.append('0'+str(normal_hr))
elif normal_hr >= 10:
normal_hr_list.append(str(normal_hr))

for military_hr in range(13, 24):
military_hr_list.append(str(military_hr))

# created dictionary with normal hrs as key and military hrs as value using zip function
hrs_am_dict = dict(zip(normal_hr_list, military_hr_list))

# printed for checking
# print('dict of normal hrs and military hrs: ',hrs_am_dict)

# iterate through the dictionary keys
for key in hrs_am_dict.keys():
# if normal hrs is equal to the key
if normal_splitted_time[hrs] == key:
# set militarys hrs equal to value of key in dict
military_splitted_time.insert(hrs, (hrs_am_dict[key]))

# set minutes and seconds of military time same as normal time
military_splitted_time.insert(mins, (normal_splitted_time[mins]))
military_splitted_time.insert(secs, (normal_splitted_time[secs]))

# printed for checking
# print('military splitted time:', military_splitted_time)

# used string join method to join strings with colon
colon = ':'
military_time = colon.join(military_splitted_time)

print('Military time: ', military_time)

user_input_time = '05:05:45PM'
# user_input_time = input('Enter time in format(HH:MM:SSAM\\PM)')
print('user input time normal time: ', user_input_time)

timeConversion(user_input_time)```