In this article, we work with a Python in-built module, datetime
which enable us to perform operations and manipulations on date and time values in python. We can pass duration or the difference we desired between two dates and receive the end date. This value can be passed in the form of days
, weeks
or months
.
Also read: How to Work with Python TimeDelta?
Dates in python
By importing the built-in python package i.e. datetime
we can work with Date function. Along with date objects we also have time objects to work with time, date and days. The datetime
module depicts date in the form of year, month, day, hour, minutes and seconds. The datetime
module has many methods to return information about the date which can be manipulated according to user eg we can use date ()
to return only date and the time.
Overview of libraries
We use datetime
and timedelta
module to add days to a given date.
datetime
: Date and Time manipulation classes are available in datetime module, the datetime
object returns date along with hour, minute and seconds.
timedelta
: The difference between two dates or time is represented by timedelta
object for example, in below implementation we pass days=5
value to timedelta
to receive a difference of 5 days from the given or current date.
Examples of adding days to Dates
Let’s now take a look at the different examples for adding days to dates in Python
Example 1: Adding days to given date using datetime
from datetime import datetime
from datetime import timedelta
#adding days to given date
given_date_string = "2030-02-01" #year-month-day format
#conversion from string to datetime object
given_date = datetime.strptime(given_date_string,"%Y-%m-%d")
#printing given date
print("The Given date: ",given_date_string)
#calculating new date
new_date = given_date + timedelta(days=6)
print("The New date: ",new_date)
In the above code, we:
- imported the
datetime
andtimedelta
modules from thedatetime
package using thefrom datetime import datetime, timedelta
statement. - assigned a string date
"2030-02-01"
to thegiven_date_string
variable. - converted the string date to a
datetime
object using thedatetime.strptime()
function, which takes two arguments: the first is the string to be converted, and the second is the format of the string. The%Y-%m-%d
format string specifies that the year, month, and day should be extracted from the string in that order. - printed the original date in string format using
print("The Given date: ", given_date_string)
. - created a new
datetime
object by adding 6 days to the original date using thetimedelta()
function. This function takes a single argument, which specifies the number of days (or other units) to add or subtract from the original date. In this case, thedays
argument is set to 6. - printed the new date in string format using
print("The New date: ", new_date)
.
If the date string is formatted in a different way you can look up to the format codes you need to pass as second argument.
Output:
The Given date: 2030-02-01
The New date: 2030-02-07 00:00:00
%a | first three letter of the weekday Sun |
%A | complete name of the weekday Sunday |
%b | first three letters of the month Oct |
%B | complete name of the month October |
Example 2: Adding days to current date using timedelta
from datetime import timedelta
from datetime import date
#adding days to current date
current_date = date.today()
#printing current date
print('Current date: ',current_date)
#calculating new date
new_date = current_date + timedelta(days=4)
#printing new date
print('New date: ',new_date)
Output:
Current date: 2023-02-12
New date: 2023-02-16
Example 3: Using date
class to add days to a date
from datetime import timedelta
from datetime import date
#using the date class to add days to a date
date = date(2023,2,12)
print("Input date: ",date)
#passing the number of days and printing the output
result = date + timedelta(days=3)
print("Result date: ",result)
Output:
Input date: 2023-02-12
Result date: 2023-02-15
Example 4: Using the date()
method to get the result date
from datetime import datetime
from datetime import timedelta
#using the date() method to get the result date
current_date = datetime.now()
print("Current date: ",current_date)
new_date = current_date + timedelta(days=5)
print(new_date) #will print 2023-02-17 23:02:39.058226
print(new_date.date()) #will print 2023-02-17
Output:
Current date: 2023-02-12 23:06:34.392314
New date: 2023-02-17 23:06:34.392314
2023-02-17
Example 5: Adding weeks to a given date
from datetime import datetime
from datetime import timedelta
from datetime import date
#adding weeks to date
date ='2023-02-12'
given_date = datetime.strptime(date,'%Y-%m-%d')
print('Given date: ',given_date)
result = given_date + timedelta(weeks = 2)
print("New date: ",result)
Output:
Given date: 2023-02-12 00:00:00
New date: 2023-02-26 00:00:00
Example 6: Adding weeks to current date
from datetime import datetime
from datetime import timedelta
from datetime import date
#adding weeks to current date
current_date = date.today()
print('Current date: ',current_date)
result = current_date + timedelta(weeks = 2)
print("New date: ",result)
Output:
Current date: 2023-02-12
New date: 2023-02-26
Example 7: Adding weeks using date()
class
from datetime import datetime
from datetime import timedelta
from datetime import date
#adding weeks using date()
current_date = date(2023,2,12)
print('Current date: ',current_date)
result = current_date + timedelta(weeks = 2)
print("New date: ",result)
Output:
Current date: 2023-02-12
New date: 2023-02-26
Real world applications of adding days to dates
- Locating files within the desired time constraint
- Handling timezones
- Printing current time, month and year.
For easy access visit this document for reference code and link.
Summary
In this article by using the datetime module or the timedelta module, we learnt how to add days to dates by passing the number of days as an input.
Recommended read
- Also read Adding a Legend to Pyplot in Matplotlib in the Simplest Manner.
- Also read How to Load Pickled Pandas Object From a File as a Path?