I have a function that creates a start and end date which I pass into a couple of other procedures when running a monthly update. The start and end dates demark a one year period, starting on the first of the month of last year, and ending on the last day of the prior month this year - so if I am running the update today the start/ends would be 9/1/2022 and 8/31/2023, respectively. I feel my approach, though it works, is a bit ham fisted and thought I'd ask if there is a more elegant way to accomplish this:
import datetime, calendar
def calc_dates():
thisday = datetime.date.today() # gets todays date, source for finding current and last month and year
thismonth = thisday.month
lastmonth = thisday.month -1
thisyear = thisday.year
lastyear = thisyear - 1
last_m_days = calendar.monthrange(thisyear,lastmonth)[1] #gets the number of days in the previous month from when the script is run
enddate = f'{lastmonth}/{last_m_days}/{thisyear}'
startdate = f'{thismonth}/1/{lastyear}'
dates = [enddate,startdate]
return dates