goolelin 发表于 2020-4-6 17:17:14

python日期判断

我想问一下python 如何判断每个月有多少号例如2019年1月有31号 2月28号   到了2020年2月是29号 如何0误差的判断本月有多少号?

zltzlt 发表于 2020-4-6 17:19:32

def days(year, month):
    if month == 2 and (year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
      return 29
    return [-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]


print(days(2020, 2))    # 2020 年 2 月
print(days(2019, 7))    # 2019 年 7 月
页: [1]
查看完整版本: python日期判断