Add Month to datetime in Python

In Python, we can represent date values using the standard datetime library. We can store all the attributes of the date along with time attributes in datetime objects. The datetime objects can be manipulated with other libraries also like dateutil, pandas, and more.

We cannot directly add or subtract datetime objects and use the datetime.timedelta object to represent such intervals of time for an addition. However, this object cannot represent intervals of months, so we need to look for alternative methods.

Ways to add month to datetime in Python

Different methods on how to add months to datetime in Python are discussed below.

Using the datetime.datetime constructor to add month to datetime in Python.

The datetime.datetime constructor is used to initializing datetime objects in Python. This constructor accepts the month and other attributes for the date and time values.

We cannot directly overwrite such objects. We can just initialize the values at first and access them.

However, we can create our own function to create a new datetime object with updated values. We will use the values from the defined object and perform some calculations to create a new object.

See the code below.

Output:

2022-08-10 00:00:00

In the above example, the add_mon() function creates a new datetime object after adding the months to a give datetime object.

Using the dateutil.relativedelta object to add month to datetime in Python

The dateutil module was developed to increase the functionalities of the standard datetime module. We can use functions from this module to work with datetime objects.

This module provides a relativedelta object to represent intervals of time, similar to the timedelta object of the datetime module. The difference between the two is that the former accepts a wide range of attributes (including months) and the latter has very few attributes.

Due to this, the relativedelta object can represent different intervals of time. We can use it to add months to datetime in Python.

For example,

Output:

2022-08-10 16:20:15

Using the pandas.DateOffset() function to add month to datetime in Python

The pandas library is used to work with DataFrame objects in Python. Columns of DataFrame can contain timestamp values that represent date and time values.

Due to this, the pandas library also has some basic functionalities for working with such values. We can use the pandas.DateOffset() function to add months to datetime in Python.

This function is used to create time intervals. With the months attribute, we can create intervals of months and add them to any given datetime object.

See the code below.

Output:

2022-07-08 15:20:15

In the above example, we adds an interval of 2 months using the pandas.DateOffset() function.

Using the arrow.shift() function to add month to datetime in Python

arrow was introduced as a third-party package that combines the functionalities of datetime and dateutil into one and provides easy-to-use functionalities.

This module has its own objects that can be used to represent date and time values. The shift() function can be used to update such objects by adding or subtracting intervals of time. We can use the months attribute in this function to add months to the object.

For example,

Output:

2022-07-09T03:44:35.343283+05:30

Conclusion

In this tutorial, we discussed several methods to add months to datetime in Python. The datetime object cannot be overwritten but we can use the values from this object to create a new object after adding the months. The timedelta object will not work in this case. The relativedelta object from the dateutil module can be used to add months to datetime in Python. Similarly, we can use the pandas.DateOffset() function as well. The arrow module is also discussed since it has a direct function to update its object and add months to it.

Was this post helpful?

Leave a Reply

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