0 points

datetime - Subtract a month from a date in Python? - Stack Overflow


PHOTO EMBED

Mon Feb 01 2021 10:40:24 GMT+0000 (UTC)

Saved by @arielvol #python

In [1]: from datetime import datetime
In [2]: import dateutil.relativedelta
In [3]: today_date = datetime.now().date()
In [4]: today_date
Out[1]: datetime.date(2016, 7, 5)
In [5]: last_month = today_date - dateutil.relativedelta.relativedelta(months=1)
In [6]: last_mont_first_date = last_month.replace(day=1)
In [7]: last_mont_first_date
Out[2]: datetime.date(2016, 6, 1)
content_copyCOPY

https://stackoverflow.com/questions/38200581/subtract-a-month-from-a-date-in-python/38200672