Hi Team,
I am new to Python. Can you please help with how to add days to a date that is in string format? please find below the sample. I would like to add 15 days to the generateddate Can you provide a sample code
This is made easy by the datetime module of the standard library.
import datetime
generated_date = "10/30/2020"
# Split the date by slashes and unpack the components.
month, day, year = generated_date.split("/")
# Create a date object. The parameters must be
# converted to integers (they are strings).
date = datetime.date(year=int(year), month=int(month), day=int(day))
# Create a delta object for the difference.
delta = datetime.timedelta(days=10)
# Add them.
new_date = date + delta
# Format the result.
new_date_formatted = f"{new_date.month}/{new_date.day}/{new_date.year}"
print(new_date_formatted)