Kodeclik Blog
How to add Days to a Date in Python
Python is a powerful programming language that has built-in functions for performing various operations, including manipulating dates and times. One common task is adding a specified number of days to a given date. In this blog post, we will explore two different ways to achieve this in Python.
If you meet your doctor today (ie May 5, 2023) and the doctor tells you to come back in two weeks, how can you compute the date of your next doctor appointment? This is a context where you will need to add a specified number of days to a given date. Let us explore the possibilities.
Method 1: Using the datetime module
The datetime module in Python provides classes for working with dates and times. To add a specified number of days to a date, we can use the timedelta class, which represents a duration or difference between two dates or times.
Here is an example of how to add 14 days to the current date using the datetime module:
import datetime
today = datetime.date.today()
come_back_in_days = 14
new_date = today + datetime.timedelta(days=come_back_in_days)
print("Current date:", today)
print("New date:", new_date)
In the above code, we first obtain the current date using the date.today() method of the datetime module. We then define the number of days to add as a variable come_back_in_days. Finally, we add the specified number of days to the current date using the timedelta(days=days_to_add) method and assign the result to a new variable new_date.
The output is as expected:
Current date: 2023-05-05
New date: 2023-05-19
Method 2: Using the dateutil module
Another way to add a specified number of days to a date in Python is to use the dateutil module, which is a third-party library that provides additional date and time functionality. In other words this is not a standard in-built default Python package.
Here is an example of how to add 14 days to a given date using the dateutil module:
import datetime
from dateutil.relativedelta import relativedelta
today = datetime.date.today()
come_back_in_days = 14
new_date = today + relativedelta(days=come_back_in_days)
print("Current date:", today)
print("New date:", new_date)
This code is exactly the same as the previous one except for the part where we add the specified number of days to the given date using the relativedelta(days=days_to_add) method of the dateutil module. The output is:
Current date: 2023-05-05
New date: 2023-05-19
In summary, adding a specified number of days to a date in Python is a common task that can be achieved in various ways. In this blog post, we have explored two methods using the datetime and dateutil modules. These methods are simple, efficient, and can be adapted to suit different use cases. Which one is your favorite?
If you liked this blogpost, tinker more with dates, such as how to find the day of the week for a given date in Python. Also learn how to add days to dates in Javascript!
Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.