Home > Software engineering >  subtract 1 month from date in python using relativedelta
subtract 1 month from date in python using relativedelta

Time:11-29

I have a date and I want subtract 1 month from this date. I'm using this code but this subtract 6 months, I don't understand why.

print('the max date : ' , all_data['DT_ANO'].max())
dt_start = all_data['DT_ANO'].max() - relativedelta(month = 1)
print('dt_start : ' , dt_start)

I get this result :

the max date :  2021-08-16 00:00:00
dt_start :  2021-01-16 00:00:00 

instead of :

the max date :  2021-08-16 00:00:00
dt_start :  2021-07-16 00:00:00 

CodePudding user response:

Guessing that relativedelta is a dateutil function, then use

relativedelta(months=1)

If you use month, year, day, ... the value will be absolute, if you use months, days, years, the value will be relative.

  • Related