补充函数 getLastDay ( y , m ),其功能是计算 y 年 m 月共有多少天。
该回答引用ChatGPT
以下是一个Python实现的 getLastDay(y, m) 函数,可以计算指定年月的天数:
import calendar
def getLastDay(y, m):
return calendar.monthrange(y, m)[1]
这个函数调用了Python内置的 calendar 模块的 monthrange() 函数,该函数可以返回指定年月的第一天是星期几以及该月的总天数。通过取返回值的第二个元素即可得到该月的总天数。
示例用法:
print(getLastDay(2022, 2)) # 输出 28
print(getLastDay(2021, 2)) # 输出 28
print(getLastDay(2021, 1)) # 输出 31
print(getLastDay(2021, 12)) # 输出 31
上述示例代码分别计算了2022年2月、2021年2月、2021年1月和2021年12月的天数。