How to add Days to a Date in Python?

29 Dec, 2020
Python provides an in-built module datetime which allows easy manipulation and modification of date and time values. It allows arithmetic operations as well as formatting the output obtained from the DateTime module. The module contains various classes like date, time, timedelta, etc. that simulate the easy implementation of dates and time (month, years, and days). 
Date and time objects are created using the DateTime module which is immutable and hashable. The following classes of the DateTime module are used to add days to a date in python :
datetime – DateTime objects give date along with time in hours, minutes, seconds. The DateTime library provides manipulation to a combination of both date and time objects (month, day, year, seconds and microseconds).timedelta – Timedelta class represents the duration. The DateTime library provides timedelta method to carry out date-related manipulation and also calculate differences in time objects. It can be majorly used to perform arithmetic operations like addition, subtraction, and multiplication. By specifying the days attribute value, we can add days to the date specified.
datetime – DateTime objects give date along with time in hours, minutes, seconds. The DateTime library provides manipulation to a combination of both date and time objects (month, day, year, seconds and microseconds).
timedelta – Timedelta class represents the duration. The DateTime library provides timedelta method to carry out date-related manipulation and also calculate differences in time objects. It can be majorly used to perform arithmetic operations like addition, subtraction, and multiplication. By specifying the days attribute value, we can add days to the date specified.
Syntax: datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
Example 1: The following Python code is used to add days to a date in Python
Python3
from datetime import datetimefrom datetime import timedelta  # taking input as the dateBegindatestring = "2020-10-11"  # carry out conversion between string # to datetime objectBegindate = datetime.strptime(Begindatestring, "%Y-%m-%d")  # print begin dateprint("Beginning date")print(Begindate)  # calculating end date by adding 10 daysEnddate = Begindate + timedelta(days=10)  # printing end dateprint("Ending date")print(Enddate)
Output:
Beginning date
2020-10-11 00:00:00
Ending date
2020-10-21 00:00:00
Example 2: The program adds 10 days to the beginning date in yyyy-mm-dd format
Python3
from datetime import datetimefrom datetime import timedeltafrom datetime import date  # taking input as the current date# today() method is supported by date # class in datetime moduleBegindatestring = date.today()  # print begin dateprint("Beginning date")print(Begindatestring)  # calculating end date by adding 4 daysEnddate = Begindatestring + timedelta(days=4)  # printing end dateprint("Ending date")print(Enddate)
Output:
Beginning date
2020-12-05
Ending date
2020-12-09
Python datetime-program
Python-datetime
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Iterate over a list in Python
Python Classes and Objects
Convert integer to string in Python
Python OOPs Concepts