美文网首页
python3内置模块calendar

python3内置模块calendar

作者: butters001 | 来源:发表于2020-11-18 14:39 被阅读0次

日历相关模块 常用方法

  • calendar.isleap(year)
    如果 year 是闰年则返回 True ,否则返回 False。
In [4]: calendar.isleap(2020)                                                                                                                              
Out[4]: True
  • calendar.leapdays(y1, y2)
    返回在范围 y1 至 y2 (包含 y1 和 y2 )之间的闰年的年数,其中 y1 和 y2 是年份。
    包括起始年,不包括结束年。
# 有五个闰年
In [9]: calendar.leapdays(2000, 2020)                                                                                                                      
Out[9]: 5
  • calendar.weekday(year, month, day)
    返回某年( 1970 -- ...),某月( 1 -- 12 ),某日( 1 -- 31 )是星期几( 0 是星期一)。
# 2表示星期三
In [5]: calendar.weekday(2020,11,18)                                                                                                                       
Out[5]: 2
  • calendar.monthrange(year, month)
    返回指定 年份 的指定 月份 的第一天是星期几和这个月的天数。
# 2020年11月的第一天是星期日 这个月一共30天
In [13]: calendar.monthrange(2020,11)                                                                                                                      
Out[13]: (6, 30)

相关文章

网友评论

      本文标题:python3内置模块calendar

      本文链接:https://www.haomeiwen.com/subject/lqjciktx.html