Time Conversion

  • + 0 comments

    def timeConversion(s): period = s[-2:] #last two digits = pm/am time = s[:-2] #without last two digits = time hms = list(map(int, time.split(':'))) #splits the list and converts str into int in one line

    if period == 'AM':
        if hms[0] == 12:
            hms[0] -= 12
    if period == 'PM':
        if not hms[0] == 12:
            hms[0] += 12
    
    hms = [f"{i:02}" for i in hms] #makes sure only two digits allowed
    return ":".join(hms) #reproducing string