I am attempting to get the date range of the previous month using Python. For example, today is January 4th, 2021 - I am looking for a way to retrieve "2020-12-01" and "2020-12-31".
I have started with the following:
today = datetime.date.today()
first = today.replace(day=1)
lastMonth = first - datetime.timedelta(days=1)
This gives me the previous month, which I can format like so:
beginningOfMonth = lastMonth.strftime("%Y-%m-01")
However, my attempts to use the calendar
module to determine the end of the month/how many days are in the month have failed. Is there an easier way to do this? Preferably without too many package deps...
Thank you in advance