[Python] ValueError: time data '24:00' does not match format '%H:%M'

Discussão em 'Python' iniciado por Stack, Fevereiro 3, 2025.

  1. Stack

    Stack Membro Participativo

    I'm having serious trouble converting 24 hour time to 12 hour.

    def standard_time(t):
    t = datetime.strptime(t, "%H:%M")
    return t


    When fed in '24:00' we get

    ValueError: time data '24:00' does not match format '%H:%M'


    I also attempt converting using %I (12 hour) instead of %H, but get an error whenever hours go over 12:

    def standard_time(t):
    t = datetime.strptime(t, "%I:%M")
    return t


    Sort of stuck...

    ValueError: time data '13:30' does not match format '%I:%M'


    Does python have a simple 24 hour to 12 hour converter? Ideally 23:00 should put out 11:00 PM and 24:00 should not throw an error!

    Continue reading...

Compartilhe esta Página