Python 计算每个月天数


以下代码通过导入calendar模块来计算每个月的天数:

  1. # Filename :test.py
  2. # author by : www.shouce.ren
  3.  
  4. import calendar
  5. monthRange = calendar.monthrange(2013,6)
  6. print(monthRange)

执行以上代码会在该输出结果为:

  1. (5, 30)

输出的是一个元组,第一个元素是月份(0-11),第二个元素是这个月的天数。以上实例输出的意思为 6 月份有 30天。