How to add and subtract days using DateTime in Python? - GeeksforGeeks

23 Apr, 2021
As we know date and time are used in programs where we have to keep track of date and time, so it is necessary to have a module to manipulate date and time. In python, a Date Time module deals with dates and times. Date time module is built into Python standard library.
Datetime module consists of the following classes:
Name
Description
For adding or subtracting date, we use something called timedelta() function which can be found under datetime class.  It is used to manipulate date, and we can perform an arithmetic operations on date like adding or subtract. timedelta is very easy and useful to implement.
Syntax: class datetime.timedelta(days=10, seconds=40, microseconds=10, milliseconds=60, minutes=10, hours=4, weeks=8)
Returns : Date
Note : if we doesn’t specify by default it takes integer as an day. 
Example 1. Adding Days 
Python3
# MANIPULATING DATETIMEfrom datetime import date, timedelta today_date = date.today() print("CURRENT DAY : ", today_date) # as said earlier it takes argument as day by defaulttd = timedelta(5)print("AFTER 5 DAYS DATE WILL BE : ", today_date + td)
 Output:  
CURRENT DAY :  2020-12-27
AFTER 5 DAYS DATE WILL BE :  2021-01-01
Example 2. Subtract Days 
Python3
# MANIPULATING DATETIMEfrom datetime import date, timedelta current_date = date.today() print("CURRENT DAY : ",current_date) print("OLD Date : ",current_date - timedelta(17))
 Output: 
CURRENT DAY :  2020-12-27
OLD Date :  2020-12-10
As in the above code, I have created a variable called current_date which holds the current date, and then prints that current date.
After that, I have used timedelta function and in the parameter, We have passed a value that how many days want to add or subtract (This value can be any integer).
Similarly, we can do the same with time also.   
Example 3: 
Python3
# Manipulate DATETIMEfrom datetime import datetime, timedeltacurrent = datetime.now()print("This is the current date and time :- ", current) # FOR PRINTING TOMORROW'S DATEtomorrow = timedelta(1)print("Tomorrow's date and time :- ", current + tomorrow) # FOR PRINTING YESTERDAY'S DATEyesterday = timedelta(-1)print("Yesterday's date and time :- ", current + yesterday)
 Output:  
This is the current date and time :-  2020-12-27 13:50:14.229336
Tomorrow's date and time :-  2020-12-28 13:50:14.229336
Yesterday's date and time :-  2020-12-26 13:50:14.229336
Example 4: 
Python3
# MANIPULATING DATETIMEfrom datetime import datetime, timedelta curr = datetime.now()print("Current Date and time :- ", curr) new_datetime = timedelta(days = 10, seconds = 40,                         microseconds = 10,                         milliseconds = 60,                         minutes = 10, hours = 4,                         weeks = 8) print("New Date and time :- ", curr + new_datetime)
Output: 
Current Date and time :-  2020-12-27 13:58:42.178211
New Date and time :-  2021-03-03 18:09:22.238221
sk9209767
simmytarika5
Picked
Python datetime-program
Python-datetime
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Create a Pandas DataFrame from Lists
Box Plot in Python using Matplotlib
Python Dictionary
Bar Plot in Matplotlib
Enumerate() in Python
Python | Get dictionary keys as a list
Python | Convert set into a list
Ways to filter Pandas DataFrame by column values
Graph Plotting in Python | Set 1
Python - Call function from another file