python subtract month from date

Solutions on MaxInterview for python subtract month from date by the best coders in the world

showing results for - "python subtract month from date"
Simon
08 May 2019
1# Normally deltatime is one of good way to manipulate date and time 
2# with fine details, but since a month can have different nubmer a days
3# relativedelta is a good option;
4from datetime import datetime
5from dateutil.relativedelta import relativedelta
6# Take a timestamps
7datetime_now = datetime.now()
8print("Current Date and time :- ", datetime_now)
9>>> Current Date and time :-  2021-08-16 11:59:49.802902
10
11# Create a delta time
12datetime_delta = relativedelta(months=+1)
13
14 # subtract  datetime_delta
15datetime_new = datetime_now - datetime_delta
16print("Current Date and time minus Delta datetime :- ", datetime_new)
17>>> Current Date and time minus Delta datetime :-  2021-07-16 11:59:49.802902