0

Here's the code to convert 12 hour time format to 24 hour format. Everything works fine except the 12 hour time AM (1:00:00 to 11:59:59) is acting weird and only returning 0.

Code :

def timeConversion(s):
    times = s.split(":")
    hour = int(times[0])
    minutes = times[1]
    seconds = times[2][:-2]
    timemod = times[-1][2:]

    military_hour=0


    if timemod=="AM" : 

        if hour == 12 : 

            miltary_hour = 0
        else:
            miltary_hour = hour

    elif timemod == "PM" : 

        if hour == 12 : 
            military_hour = hour
        else:
            military_hour = hour + 12



    if military_hour < 10 : 
        military_hour = "0" + str(military_hour)
    else:
        military_hour = str(military_hour)


    return ":".join([military_hour,minutes,seconds])

## below 2 are incorrect 
print(timeConversion("9:30:10AM"))
print(timeConversion("6:00:00AM"))

## these are correct 
print(timeConversion("12:00:00AM"))
print(timeConversion("12:00:00PM"))
print(timeConversion("11:59:59PM"))

The solution is

00:30:10                                                                                                                                                                                                                   
00:00:00                                                                                                                                                                                                                   
00:00:00                                                                                                                                                                                                                   
12:00:00                                                                                                                                                                                                                   
23:59:59

Why the heck AM time before noon always returns 00 instead of actual hour ?

2
  • your if else block is identical in AM case? Commented Jun 4, 2020 at 5:47
  • I had typo. I have edited the code which still has the problem. Commented Jun 4, 2020 at 5:52

2 Answers 2

2

You have misspelled military_hour in the AM logic.

Here:

if timemod=="AM" : 
    if hour == 12 : 
        miltary_hour = 0
    else:
        miltary_hour = hour
Sign up to request clarification or add additional context in comments.

6 Comments

you are the best. Me being sleepy can't think clearly. That was indeed that "silly" mistake in the code. I see correct results after fixing variable name.
@Prithi. I recommend deleting the question. This is too specific to be of much use to anyone else
@paradoxlover. I think so, assuming OP can delete with your answer present. But you'll get it back quickly it seems.
@MadPhysicist Right... The community comes first. And yes, I will get it back ;).
can't delete. Stack Overflow doesn't let me. I guess have to stick around with this one.
|
-1

you can try this:

time_12 = input().strip()
time_in_pm = time_12[-2:].lower() == 'pm'
time_list = list(map(int, time_12[:-2].split(':')))
if not time_in_pm and time_list[0] == 12:
    time_list[0] = 0
if time_in_pm and time_list[0] < 12:
    time_list[0] += 12
print(':'.join(map(lambda x: str(x).rjust(2, '0'), time_list)))

or You can also use the python date time library.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.