Last active
July 17, 2021 17:48
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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