[Python] Parse and Convert a mal-formatted Date/Time String to a Formatted DateTime using...

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

  1. Stack

    Stack Membro Participativo

    What is the best method for formatting "12/14/24 9a-1p" or "12/14/24 10p-4a"

    from datetime import datetime
    import parsedatetime as pdt # $ pip install parsedatetime

    cal = pdt.Calendar()

    print("now: %s" % now)
    for time_string in ["12/14/24 9a-1p", "12/14/24 10p-4a","6/1/24 6a"]:
    print("%s:\t%s" % (time_string, cal.parseDT(time_string, now)[0]))


    Output:

    12/14/24 9a-1p: 2024-12-14 13:00:00
    12/14/24 10p-4a: 2024-12-14 04:00:00
    6/1/24 6a: 2024-06-01 06:00:00


    I found that from a different answer but it was 10 years old.

    Q: Is there a better option in 2025 that can get this done including the 12/14/24 9a-1p formatted strings?

    Continue reading...

Compartilhe esta Página