Python内置模块---日历模块

日历模块

code:

# ***************************************************
"""
日历模块:
    calendar.monthrange()---获取指定年份和月份的数据(第一天时周几,月份的天数)
"""
import calendar
import time,os


def showmonth(year, month):
    res = calendar.monthrange(year, month)
    days = res[1]  # 当前月份的天数
    w = res[0]  # 当前月份第一天是星期几
    print(f"*****{year}{month}月的日历信息*****")
    print(" 一  二   三  四   五  六  日 ")
    print("***************************")
    d = 1
    print("    " * w, end="")
    while d <= days:
        print(' {:0>2d} '.format(d), end="")
        if (d + w) % 7 == 0:
            print("\n", end="")
        d += 1
    print("\n***************************")


dd = time.localtime()
year = dd.tm_year
month = dd.tm_mon
showmonth(year,month)
while True:
    print("    <上一月       下一月>    ")
    res = input("请输入< or >选择:")
    os.system('cls')
    if res == '<':
        month -= 1
    elif res == '>':
        month += 1
    else:
        month = month
    if month == 0:
        year -= 1
        month = 12
    elif month == 13:
        year += 1
        month = 1
    showmonth(year, month)

运行结果:

E:\Programs_Way\Python\python.exe D:/Prj/_PythonSelf/Study_Basic_Grammar/_60Build_in_module_calendar.py
*****2022年1月的日历信息*****
 一  二   三  四   五  六  日 
***************************
                     01  02 
 03  04  05  06  07  08  09 
 10  11  12  13  14  15  16 
 17  18  19  20  21  22  23 
 24  25  26  27  28  29  30 
 31 
***************************
    <上一月       下一月>    
请输入< or >选择:<
*****2021年12月的日历信息*****
 一  二   三  四   五  六  日 
***************************
         01  02  03  04  05 
 06  07  08  09  10  11  12 
 13  14  15  16  17  18  19 
 20  21  22  23  24  25  26 
 27  28  29  30  31 
***************************
    <上一月       下一月>    
请输入< or >选择:>
*****2022年1月的日历信息*****
 一  二   三  四   五  六  日 
***************************
                     01  02 
 03  04  05  06  07  08  09 
 10  11  12  13  14  15  16 
 17  18  19  20  21  22  23 
 24  25  26  27  28  29  30 
 31 
***************************
    <上一月       下一月>    
请输入< or >选择:(这里可以一直循环下去噢嘿嘿)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jasmine-Lily

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值