Python Sub Months to Date Example

Published On: 10/06/2022 | Category: Python

Hi Dev,

This article will give you example of python sub months to a date. I explained simply about current date minus 1 month to date python. This article goes in detailed on sub months to date python datetime. you can see sub months to a date python.

Here I will give an example for how to sub months to date in python. I am use datetime and time module to sub months date.

So let's see the below example:

Example : 1
# current date minus a month
from datetime import datetime
from dateutil.relativedelta import relativedelta

# minus a month
currentTimeDate = datetime.now() - relativedelta(months=1)
currentTime = currentTimeDate.strftime('%Y-%m-%d')
print(currentTime)
Output:
2022-05-10
Example : 2
# current date minus a month
from datetime import datetime
from dateutil.relativedelta import relativedelta

# minus number of month
currentTimeDate = datetime.now() - relativedelta(months=3)
currentTime = currentTimeDate.strftime('%Y-%m-%d')
print(currentTime)
Output:
2022-03-10

It will help you....