[Python] Python - Get Yesterday's date as a string in YYYY-MM-DD format

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

  1. Stack

    Stack Membro Participativo

    As an input to an API request I need to get yesterday's date as a string in the format YYYY-MM-DD. I have a working version which is:

    yesterday = datetime.date.fromordinal(datetime.date.today().toordinal()-1)
    report_date = str(yesterday.year) + \
    ('-' if len(str(yesterday.month)) == 2 else '-0') + str(yesterday.month) + \
    ('-' if len(str(yesterday.day)) == 2 else '-0') + str(yesterday.day)


    There must be a more elegant way to do this, interested for educational purposes as much as anything else!

    Continue reading...

Compartilhe esta Página