Home ยป How to add or subtract days to a date in Python

How to add or subtract days to a date in Python

1 Year Subscription
Java SE 11 Programmer II [1Z0-816] Practice Tests
Oracle Java Certification
Java SE 11 Developer (Upgrade) [1Z0-817]
Java SE 11 Programmer I [1Z0-815] Practice Tests
Spring Framework Basics Video Course

In this example we will show you how to add or subtract days to a date in Python

Example

The timedelta() method takes the number of days to be added as its argument and returns them in a date format that is year – month – date.

We will add 5 days to todays date, then for that changed date and we will then subtract 5 days

from datetime import timedelta, date

# go forward 5 days
Date_forward = date.today() + timedelta(days=5)
print(Date_forward)

# go backward 5 days
Date_backward = Date_forward + timedelta(days=-5)
print(Date_backward)

When you run this you will see something like this

>>> %Run adddays.py
2022-04-23
2022-04-18

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More