[Python] How to modify a time with datetime.timedelta and then format it with...

Discussão em 'Python' iniciado por Stack, Abril 28, 2025.

  1. Stack

    Stack Membro Participativo

    I can find the date of next Sunday (or today if today is Sunday) just fine using timedelta with the line:

    print(datetime.date.today() + datetime.timedelta(7 - int(time.strftime("%u"))))


    And I can take today's date and format it the way I want just fine using strftime with the line:

    print(datetime.date.today().strftime("%d.%m.%Y"))


    But I can't for the life of me figure out how to get these two to play well together so I can have next Sunday's date in the format I want. The result I get from running these two individually is

    2025-05-04
    27.04.2025


    but I want 05.04.2025. I'm aware I could feed the 2025-05-04 into str() and then format it but that feels clunky. I was hoping to find out how to use strftime in this situation because I'm pretty sure what I'm trying to do is exactly what it's intended for.

    Continue reading...

Compartilhe esta Página