Skip to content

Instantly share code, notes, and snippets.

@faulander
Last active July 17, 2021 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faulander/7250686686d3c822a51f1d902b1261d1 to your computer and use it in GitHub Desktop.
Save faulander/7250686686d3c822a51f1d902b1261d1 to your computer and use it in GitHub Desktop.
[Function to return all dates for ALL weeks in a given month] #python
import copy
import calendar
import pendulum as p
def getMonthDates(year:int, month:int, includeWeekends:bool = False) -> dict[list[p.datetime]]:
monthdates = list()
cal = calendar.Calendar()
dates = {}
monthdates = []
for countweek, week in enumerate(cal.monthdatescalendar(year,month)):
for day in week:
parsedDay = p.datetime(day.year,day.month,day.day)
if includeWeekends:
monthdates.append(parsedDay)
elif (parsedDay.day_of_week != p.SATURDAY and parsedDay.day_of_week != p.SUNDAY):
monthdates.append(parsedDay)
dates[countweek] = copy.deepcopy(monthdates)
monthdates.clear()
return(dates)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment