Answer to Question #311526 in Python for mohan

Question #311526

write a profram to convert the date in string fromat to another string format.

the input date string format is like "jul 01 2014 02:43PM"

the output date string format should be like "DD/MM/YYYY HH:MM:SS"


1
Expert's answer
2022-03-14T13:30:05-0400
Months = {'jan':1, 'feb':2, 'mar':3, 'apr':4, 'may':5, 'jun':6,
          'jul':7, 'aug':8, 'sep':9, 'oct':10, 'nov':11, 'dec':12}

line = input()
tokens = line.split()
month = Months[tokens[0].lower()]
day = int(tokens[1])
year = int(tokens[2])
time = tokens[3]
tokens = time.split(':')
hr = int(tokens[0])
min = int(tokens[1][:2])
if len(tokens) > 2:
    sec = int(tokens[2][:2])
else:
    sec = 0
if tokens[-1][-2:].lower() == 'pm':
    hr += 12

s = f'{day:02d}/{month:02d}/{year} {hr:02d}:{min:02d}:{sec:02d}'
print(s)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS