• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Python Add Days to Date Using datetime timedelta() Function

Python Add Days to Date Using datetime timedelta() Function

February 8, 2022 Leave a Comment

To add days to date using Python, the easiest way is to use the Python timedelta() function from the datetime module.

from datetime import timedelta, date

two_days_in_future = date.today() + timedelta(days=2)

print(date.today())
print(two_days_in_future)

#Output:
2022-02-08
2022-02-10

When working with data in Python, many times we are working with dates. Being able to manipulate and change dates easily is very important for efficient processing.

One such change is to be able to add days to a date.

With Python, we can easily add days to a date with the help of the datetime module.

To add days to a date in Python, we can use the timedelta() function from the datetime module.

Below is code that shows you how to add days to get some dates in the future using Python.

from datetime import timedelta, date

two_days_in_future = date.today() + timedelta(days=2)
ten_days_in_future = date.today() + timedelta(days=10)
one_hundred_days_in_future = date.today() + timedelta(days=100)

print(date.today())
print(two_days_in_future)
print(ten_days_in_future)
print(one_hundred_days_in_future)

#Output:
2022-02-08
2022-02-10
2022-02-18
2022-05-19

You can also use datetimes in the same way to add days to a datetime variable.

Below are some examples of adding days to datetime variables in Python.

from datetime import timedelta, datetime

now = datetime.now()

two_days_in_future = now  + timedelta(days=2)
ten_days_in_future = now  + timedelta(days=10)
one_hundred_days_in_future = now  + timedelta(days=100)

print(now)
print(two_days_in_future)
print(ten_days_in_future)
print(one_hundred_days_in_future)

#Output:
2022-02-08 09:17:59.577000
2022-02-10 09:17:59.577000
2022-02-18 09:17:59.577000
2022-05-19 09:17:59.577000

If instead you’d like to subtract days from a date in Python, you can use a very similar method as shown above but subtract the timedelta function.

Adding One Day to Get Tomorrow’s Date Using Python

We can easily get tomorrow’s date using the Python datetime module. To get tomorrow’s date, we just need to add 1 day using the timedelta() function.

Below is the Python code which will allow you to get tomorrow’s date.

from datetime import timedelta, date

tomorrow_date = date.today() - timedelta(days=1)

print(date.today())
print(tomorrow_date )

#Output:
2022-02-08
2022-02-09

How to Add Days to a Date With pandas in Python

If you are using the Python pandas module, we can add days to a date easily.

With pandas, to add days to a date, we use the DateOffset() function.

Below is an example of how to use pandas to add days to a date in Python.

import pandas as pd

startdate = "01/29/2022"
enddate = pd.to_datetime(startdate) + pd.DateOffset(days=5)

print(startdate)
print(enddate)

#Output:
2022-01-29 00:00:00
2022-02-03 00:00:00

Hopefully this article has been beneficial for you to learn how to add days to a date using Python.

Other Articles You'll Also Like:

  • 1.  Write Inline If and Inline If Else Statements in Python
  • 2.  How to Check if Number is Negative in Python
  • 3.  Why Dictionaries Can’t Have Duplicate Keys in Python
  • 4.  Zip Two Lists in Python
  • 5.  How to Remove All Occurrences of a Character in a List Using Python
  • 6.  pandas sum – Get Sum of Series or DataFrame Columns
  • 7.  Using Python to Get Queue Size
  • 8.  PROC MIXED Equivalent in Python for Least Squared Means ANOVA
  • 9.  Find First Occurrence in String of Character or Substring in Python
  • 10.  Changing Python Turtle Speed with speed() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy