I'm trying to find last business day of of the month. I wrote the code below for that and it works fine but I was wondering if there is a cleaner way of doing it?
from datetime import date,timedelta
import datetime
import calendar
today=datetime.date.today()
last = today.replace(day=calendar.monthrange(today.year,today.month)[1])
if last.weekday()<5:
print last
else:
print last-timedelta(days=1+last.weekday()-5)
Thanks in advance!
I use this for the first business day of the month but it can be used for last business day of the month as well:
I left some code in there that is not needed for the last business day of the current month, but may be useful to someone.
I use the following:
Let's say you want to get the last business days of the month up-to the end of the next two years, the following will work.
You can use
Pandas
to get business days. Refer http://pandas.pydata.org/pandas-docs/stable/timeseries.htmlAlso you can refer this https://pypi.python.org/pypi/business_calendar/ for simple business days calculation.