Python Calculate Days in Each Month

Document 对象参考手册Python3 Examples

The following code calculates the number of days in each month by importing the calendar module:

Example (Python 3.0+)

#!/usr/bin/python3 # author by : www.example.com import calendar monthRange = calendar.monthrange(2016,9) print(monthRange)

Executing the above code produces the following output:

(3, 30)

The output is a tuple. The first element is the day of the week (0-6) corresponding to the first day of the queried month, and the second element is the number of days in that month. The output in the above example means that the first day of September 2016 is Thursday, and the month has 30 days in total.

Document 对象参考手册Python3 Examples

Other Extensions