[Python] 0-23 hour military clock to standard time (hh:mm)

Discussão em 'Python' iniciado por Stack, Maio 27, 2025.

  1. Stack

    Stack Membro Participativo

    How would I go about this process? Let's say that someone types in "3:5", and I should be able to get the output "3:05 AM". Or if someone types in "00:00", I should get "12:00 AM". I realize the plethora of ways to go about this if the time format was simply input as "hhmm", but the input can range from "h:m", "hh:m", "hh:m", and "hh:mm". So it's very situational.

    def timeConvert():
    miliTime = int(input("Enter a time in hh:mm (military) format: "))
    miliTime.split(":")
    if len(miliTime) == 3:
    hours = miliTime[0]
    minutes = miliTime[2]
    if hours < 0:
    print("Hours can't be less than 0.")
    elif len(miliTime) == 4:
    if miliTime[0:2] >= 10:
    hours = militime[0:2]
    minutes = militime[3]
    if minutes < 0:
    print("Minutes can't be less than 0.")
    if minutes < 10:
    minutes = 0 + minutes
    else:
    hours = miliTime[0]
    minutes = militime[2:]
    if minutes >= 60:
    print("Too big of a number for minutes.")
    else:
    hours = miliTime[0:2]
    minutes = miliTime[3:]
    setting = AM
    if hours > 12:
    setting = PM
    hours -= 12
    print(hours + ":" + minutes + setting)

    timeConvert()

    Continue reading...

Compartilhe esta Página