As Peter mentioned, calendar.monthrange(year, month)
returns weekday (0-6 ~ Mon-Sun) and number of days (28-31) for year
, month
.
>>> import calendar
>>> print calendar.monthrange(2012,1)[1]
31
>>> calendar.monthrange(2012,2)[1]
29
Edit: updated answer to return the number of days of the current month
>>> import calendar
>>> import datetime
>>> now = datetime.datetime.now()
>>> print calendar.monthrange(now.year, now.month)[1]
29