In my previous article, we have seen how to add days to a date object in python. Now we will see how to subtract days form date object using the datetime.timedelta() method. Here is an example.
## Subtracting days from date
import datetime
## Date object
date_original = datetime.date(2019, 7, 20)
## Days to subtract
days_to_sub = 387
## Subtract
date_new = date_original - datetime.timedelta(days_to_sub)
print("\n Original Date: ", date_original, "\n")
print("\n New Date: ", date_new, "\n")
Reference
- About datetime.timedelta at Python Docs.
days_to_add seems like a misleading name for days to subtract đ
Hello Moss,
Thank you for the feedback. I’ve corrected it now.