• 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
  • Ruby
  • About
You are here: Home / Python / Python Add Months to Datetime Variable Using relativedelta() Function

Python Add Months to Datetime Variable Using relativedelta() Function

February 9, 2022 2 Comments

To add months to a datetime variable using Python, the easiest way is to use the Python relativedelta() function from the dateutil module.

from datetime import datetime
from dateutil.relativedelta import relativedelta

now = datetime.now()

two_months_in_future = now + relativedelta(months=2)

print(now)
print(two_months_in_future)

#Output:
2022-02-09 08:58:25.940729
2022-04-09 08:58:25.940729

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 months to a datetime variable.

With Python, we can easily add months to a datetime variable with the help of the datetime module and dateutil module.

To add months to a datetime in Python, we can use the relativedelta() function from the dateutil module.

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

from datetime import datetime
from dateutil.relativedelta import relativedelta

now = datetime.now()

two_months_in_future = now + relativedelta(months=2)
six_months_in_future = now + relativedelta(months=6)
one_year_in_future = now + relativedelta(months=12)

print(now)
print(two_months_in_future)
print(six_months_in_future)
print(one_year_in_future)

#Output:
2022-02-09 08:58:25.940729
2022-04-09 08:58:25.940729
2022-08-09 08:58:25.940729
2023-02-09 08:58:25.940729

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

You can also use dates with the code above. The difference between dates and datetimes are that dates don’t have a time associated with them.

from datetime import date
from dateutil.relativedelta import relativedelta

now = date,today()

two_months_in_future = now + relativedelta(months=2)
six_months_in_future = now + relativedelta(months=6)
one_year_in_future = now + relativedelta(months=12)

print(now)
print(two_months_in_future)
print(six_months_in_future)
print(one_year_in_future)

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

How to Add Months to a Date With pandas in Python

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

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

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

import pandas as pd

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

print(startdate)
print(enddate)

#Output:
2022-01-29 00:00:00
2022-06-29 00:00:00

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

Other Articles You'll Also Like:

  • 1.  Using Python to Count Even Numbers in List
  • 2.  How to Measure Execution Time of Program in Python
  • 3.  Multiply Each Element in List by Scalar Value with Python
  • 4.  Python os.sep – Create Operating System Path Seperator Character
  • 5.  Using Python to Get All Combinations of Two Lists
  • 6.  Using Python to Add String to List
  • 7.  Solve Python Object Does Not Support Indexing Error
  • 8.  Convert String into Tuple in Python
  • 9.  Using Python to Add Items to Set
  • 10.  Python Check if Dictionary Value is Empty

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

Comments

  1. john zhang says

    June 6, 2022 at 9:55 am

    great website for Python studies. Please keep up the good work

    Reply
    • Erik says

      June 6, 2022 at 9:58 am

      Thanks for the compliment! Glad you are getting value from it.

      Reply

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy