In this article, let’s delve into the techniques for Add Months to datetime Object in Python. Working with dates and times often requires manipulation and adjustment, and understanding how to add months to a datetime object is a crucial skill. We will explore various methods and libraries to achieve this task, providing you with a comprehensive guide on enhancing the flexibility of datetime objects in Python.
Add Months to datetime Object in Python
In Python, there are various methods to Add Months to datetime Object in Python. In this discussion, we will explore some commonly used techniques along with illustrative examples.
- Using NumPy Library
- Using relativedelta
- Using Panda’s Library
- By Creating new Function
Add Months to datetime using NumPy Library
In this example we use the NumPy library, to create a datetime object using the np.datetime64() method and then add months using timedelta using the np.timedelta64() method. A string of date format is passed in the np.datetime64() method and the required number of months is added using the np.timedelta64() method.
# import packages import numpy as np
# adding months to a given date print ( 'old date is : ' + str (np.datetime64( '2022-04' )))
new_date = np.datetime64( '2022-04' ) + np.timedelta64( 5 , 'M' )
print ( 'new date is : ' + str (new_date))
|
Output:
old date is : 2022-04 new date is : 2022-09
Add Months to datetime using relativedelta
In this example, we use datetime and dateutil packages. The current date is known by using the datetime.date() method is used to create a date object by specifying the year, month, and day, and by using the relativedelta() method we add the number of months, and finally, we get a new datetime object.
# import packages from datetime import date
from dateutil.relativedelta import relativedelta
# adding months to a particular date print ( 'date : ' + str (date( 2020 , 5 , 15 )))
new_date = date( 2020 , 5 , 15 ) + relativedelta(months = 5 )
print ( 'new date is : ' + str (new_date))
|
Output:
date : 2020-05-15
new date is : 2020-10-15
Add Months to datetime using Panda’s Library.
In this example, we import the pandas’ package. In pandas, a string is converted to a datetime object using the pd.to_datetime() method and pd.DateOffset() method is used to add months to the created pandas object. finally, a new datetime object is created.
# import packages import pandas as pd
# adding months to a particular date present = '2022-05-05'
print ( 'date : ' + present)
new_date = pd.to_datetime(present) + pd.DateOffset(months = 5 )
print ( 'new date is : ' + str (new_date))
|
Output:
date : 2022-05-05 new
date is : 2022-10-05 00:00:00
Add Months to datetime by Creating New Function
In this example, the add_months
function takes a datetime
object and the number of months to add. It then calculates the new year and month values and creates a new datetime
object with these updated values. Finally, it prints the original and new dates for verification.
from datetime import datetime, timedelta
def add_months(current_date, months_to_add):
new_date = datetime(current_date.year + (current_date.month + months_to_add - 1 ) / / 12 ,
(current_date.month + months_to_add - 1 ) % 12 + 1 ,
current_date.day, current_date.hour, current_date.minute, current_date.second)
return new_date
# Example usage current_date = datetime( 2023 , 1 , 15 , 12 , 30 , 45 )
months_to_add = 3
new_date = add_months(current_date, months_to_add)
print (f "date is : {current_date}" )
print (f "date is : {new_date}" )
|
Output:
date is : 2023-01-15 12:30:45
date is : 2023-04-15 12:30:45