I have a use case like below in Python
Find the 1st business day and last business day of the previous month based on a date.
For example if date is 2024-06-10
first_business_day = '2024-05-01'
last_business_day = '2024-05-31'
I have tried like below
run_date = '2024-06-10'
from datetime import datetime, timedelta, date
import datetime
d = datetime.datetime.strptime(run_date, '%Y-%m-%d').date()
previous_month_first_business_day = (d - timedelta(days=d.day)).replace(day=1).strftime("%Y-%m-%d")
# result is below
previous_month_first_business_day = '2024-05-01'
previous_month_last_business_day = (d - timedelta(days=d.day)).strftime("%Y-%m-%d")
# result is below
previous_month_last_business_day = '2024-05-31'
This is working fine for month of May
. But when I want the same result for June
Then using the above I am getting
# result is below
previous_month_first_business_day = '2024-06-01' # This should be '2024-06-03'
previous_month_last_business_day = (d - timedelta(days=d.day)).strftime("%Y-%m-%d")
# result is below
previous_month_last_business_day = '2024-06-30' # This should be '2024-06-29'
What should I do to achieve the right result