Python: Find the number of days in a month

To figure out the month range, you can use a built-in function from the calendar library.

from calendar import monthrange

range = monthrange(year, month)
(4, 31)

The result will be a tuple, the first value containing the index of the first day of the month and second value holding the number of days in the month.

*update 31/05/2011*

The first value is used to know what day of the week the first day of the month is.

  • 0 = Monday
  • 1 = Tuesday
  • 3 = Wednesday
  • ...
  • 6 = Sunday

It will correspond with the values in the "calendar" library.

calendar.MONDAY = 0, calendar.SUNDAY = 6

[ Source ]

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog