Python Tips – Number of weeks in a month

# calculate the number of weeks in a month
# based on the ISO calendar

import datetime
import calendar

today =

firstday =,today.month, 1)

# calendar.monthrange returne a tuple (weekday of first day of the month, lastday of month)
lastday =,today.month, calendar.monthrange(today.year,today.month)[1])

# isocalendar returns a tuple - (isoyear, isoweeknumber, isodaynumber)
weeks = lastday.isocalendar()[1] - firstday.isocalendar()[1]

Leave a comment

Your email address will not be published. Required fields are marked *