[Python] python string to date ISO 8601

Discussão em 'Python' iniciado por Stack, Julho 10, 2025.

  1. Stack

    Stack Membro Participativo

    I am converting many obscure date formats from an old system. The dates are unpacked/processed as strings and converted into ISO 8601 format.

    This particular function attempts to convert YYMMDD0F to YYYYMMDD -- function name says it all. Dates from the year 2000 make this messy and clearly this is not the most pythonic way of handling them. How can I make this better using the dateutil.parser?

    def YYMMDD0FtoYYYYMMDD(date):
    YY = date[0:2]
    MM = date[2:4]
    DD = date[4:6]
    if int(YY) >= 78:
    YYYY = '19%s' % YY
    elif 0 <= int(YY) <= 77 and MM!='' and MM!='00':
    YYYY = '20%s' % YY
    else:
    YYYY = '00%s' % YY
    return "%s-%s-%s" % (YYYY, MM, DD)

    Continue reading...

Compartilhe esta Página