Q:

Subtract a week (7 days) from a given date in Python

belongs to collection: Python Date and Time Exercises

0

Subtract a week (7 days)  from a given date in Python

Given:

given_date = datetime(2020, 2, 25)

Expected output:

2020-02-18

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Solution:

from datetime import datetime, timedelta

given_date = datetime(2020, 2, 25)
print("Given date")
print(given_date)

days_to_subtract = 7
res_date = given_date - timedelta(days=days_to_subtract)
print("New Date")
print(res_date)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Print a date in a the following format using pytho... >>
<< Convert string into a datetime object using python...