Python: Find the number of days in a month

3 comments

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 ]

3 comments:

  1. What is "number of the first day of the month"? What does it means?

    ReplyDelete
  2. Hi there,

    I just updated the post to make it easier to understand.

    ReplyDelete
  3. Thnks! It's useful for me

    ReplyDelete

Leave your thoughts ...
---
If you are having trouble with copy/pasting in comments, you need to sign in or click 'Preview'. For more information about this Firefox bug, see here.

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