How to increment datetime by custom months in python without using library [duplicate]

This is short and sweet method to add a month to a date using dateutil’s relativedelta.

from datetime import datetime
from dateutil.relativedelta import relativedelta
    
date_after_month = datetime.today()+ relativedelta(months=1)
print('Today: ',datetime.today().strftime('%d/%m/%Y'))
print('After Month:', date_after_month.strftime('%d/%m/%Y'))
Today:  01/03/2013

After Month: 01/04/2013

A word of warning: relativedelta(months=1) and relativedelta(month=1) have different meanings. Passing month=1 will replace the month in original date to January whereas passing months=1 will add one month to original date.

Note: this will require python-dateutil module. If you are on Linux you need to run this command in the terminal in order to install it.

sudo apt-get update && sudo apt-get install python-dateutil

Explanation : Add month value in python

Leave a Comment