Search code examples
pythondatedatetimetimedelta

How to add a 1 day to today's date in python?


What i'm trying to do is to add one extra day to today's date and have the outcome match this formula "%Y-%m-%d" and nothing else. I want the printed results to match this yyyy-mm-dd

from datetime import datetime, timedelta, date

s = date.today()
date = datetime.strptime(s, "%Y-%m-%d")
modified_date = date + timedelta(days=1)
datetime.strftime(modified_date, "%Y-%m-%d")

print(modified_date)

Solution

  • You are trying to do date operations on strings and not using the result of your formatting call:

    s = date.today()
    modified_date = s + timedelta(days=1)
    modified_date = modified_date.strftime("%Y-%m-%d")  # this would be more common
    # modified_date = datetime.strftime(modified_date, "%Y-%m-%d")
    
    print(modified_date)