How to accurately minus X month on a date in Python?

Viewed 8852

So this code what I want:

import datetime
d = datetime.date.today()

three_months_ago = d - timedelta(months=3)

However, as we know, the 'months' param does not exist in timedelta.

I admit I can program like this to achieve the goal:

if d.month > 3:
    three_months_ago = datetime.date(d.year, d.month-3, d.day)
else:
    three_months_ago = datetime.date(d.year-1, d.month-3+12, d.day)

But this seems really stupid...

Can you guys tell me how to realize this smartly?

2 Answers
Related