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 1. date It shows the date according to the Georgian calendar with attributes are year, month and day. 2. time It shows time, independent of any particular day with attributes are hour, minute, second, microsecond, and tzinfo. 3. datetime It is a collection of date and time with the attributes year, month, day, hour, minute, second, microsecond, and tzinfo. 4. timedelta It is used to manipulate date. 5. tzinfo It provides information about time zone.
Add and subtract days using DateTime in Python
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, timedeltatoday_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, timedeltacurrent_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, timedeltacurr = 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
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


