python Calendar日历模块函数介绍

1、calendar.calendar(year,w=2,l=1,c=6):返回year年年历

返回一个多行字符串格式的year年年历,3个月一行,间隔距离为c。
在这里插入图片描述

2、calendar.firstweekday( ):返回当前每周起始日期的设置。

默认情况下,首次载入 calendar 模块时返回 0,即星期一。

3、calendar.setfirstweekday(weekday):设置每周的起始日期码
import calendar
c0 = calendar.firstweekday()   # 输出:0
calendar.setfirstweekday(1)   # 设置周一为1
c1 = calendar.firstweekday()  # 输出:1
4、 calendar.isleap(year) :闰年返回 True,否则为 False。
print(calendar.isleap(2022))  # False
5、 calendar.leapdays(y1,y2) :返回在Y1,Y2两年之间的闰年总数
print(calendar.leapdays(2000,2022))  # 6
6、calendar.month(year,month,w=2,l=1):返回year年month月日历

返回一个多行字符串格式的year年month月日历,两行标题,一周一行。每日宽度间隔为w字符。每行的长度为7* w+6。l是每星期的行数。

print(calendar.month(2022, 9, w=2, l=1))

输出:
在这里插入图片描述

7、 calendar.monthcalendar(year,month)

返回一个整数的单层嵌套列表。每个子列表装载代表一个星期的整数。Year年month月外的日期都设为0;范围内的日子都由该月第几日表示,从1开始。

print(calendar.monthcalendar(2022,9))
输出:
[[0, 0, 0, 1, 2, 3, 4], [5, 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24, 25], [26, 27, 28, 29, 30, 0, 0]]
8、calendar.monthrange(year,month)—返回(month月第一天是星期几,month月天数)

返回的是一个元组数据(两个整数)。
第一个整数:代表本月起始星期数(0:星期一 … 6:星期天)
第二个整数:代表本月最后一天的日期数,即该月天数

print(calendar.month(2022, 9))
print(calendar.monthrange(2022, 9))  # (3, 30)

输出:
   September 2022
Mo Tu We Th Fr Sa Su
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30

(3, 30)
9、calendar.monthlen( year , month ):返回month 月天数
print(calendar.monthlen(2022,9))  # 30
10、calendar.weekday(year,month,day):返回给定日期的日期码。

0(星期一)到6(星期日)。

print(calendar.month(2022, 9))
# 输出2022-9-30号对应的日期码
print(calendar.weekday(2022,9,30))  # 4 

输出:
   September 2022
Mo Tu We Th Fr Sa Su
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30

4
11、calendar.prevmonth(year, month):返回month的上一个月year, month-1)
12、calendar.nextmonth(year, month):返回month的下一个月(year, month+1)
print(calendar.prevmonth(2022, 1))   # (2021, 12)
print(calendar.prevmonth(2022, 4))   # (2022, 3) 
print(calendar.nextmonth(2022, 12))  # (2023, 1)
print(calendar.nextmonth(2022, 9))   # (2022, 10)
  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值