python - Get amount of weekday X in a month -
how determine number of days of weekday x in python?
e.g. number of mondays in september 2014.
example usage standard 0
monday, 6
sunday calendar scheme:
>>> numberofweekdays(2014, 9, weekday=0) 5
edit
using accepted answer:
def numberofweekdays(year, month, weekday=calendar.monday): return sum(1 week in calendar.monthcalendar(year, month) if week[weekday])
there's various ways calendar
. e.g.
import calendar month = (2014, 9) # october, 2014 sum(1 week in calendar.monthcalendar(*month) if week[calendar.monday]) out[28]: 5 sum(1 week in calendar.monthcalendar(*month) if week[calendar.friday]) out[29]: 4
Comments
Post a Comment