Python calendar.weekday()方法 (Python calendar.weekday() Method)

weekday() method is an inbuilt method of the calendar module in Python. It works on simple text calendars and returns what is the day of the week for the given year, month and day mentioned in the function argument. Here Monday denotes 0 and incrementing the following days by one for the year(1970–…), month (1–12), day (1–31).

weekday()方法是Python中日历模块的内置方法。 它适用于简单的文本日历,并返回函数参数中给定的年,月和日的星期几。 在这里,星期一表示0,然后将年(1970–…),月(1–12),天(1–31)的第二天增加1。

Module:

模块:

    import calendar

Syntax:

句法:

    weekday(year, month, day)

Parameter(s):

参数:

  • year: It is a required parameter, which represents the year value of the calendar

    year :这是必填参数,代表日历的年份

  • month: It is a required parameter, which represents the month value of the calendar

    month :这是必填参数,代表日历的月份值

  • day: It is a required parameter, which represents the day of the month.

    day :这是必填参数,代表月份中的一天。

Return value:

返回值:

The return type of this method is <class 'int'>, it returns a number which is the day on this given year, month and day. Monday is 0 and Sunday is 6.

此方法的返回类型为<class'int'> ,它返回一个数字,该数字是该给定的年,月和日的日期。 星期一是0,星期日是6。

Example:

例:

# Python program to illustrate the
# use of weekday() method
# importing calendar module
import calendar
year = 2020
month = 2
day = 20
x = calendar.weekday(year, month, day)
print("Weekday number for the given date:", x)
print()
# We can also make a list of days and
# print the day name accordingly
wday = ['Monday', "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
year = 1996
month = 10
day = 27
x = calendar.weekday(year, month, day)
print("Weekday number:", x)
print("Weekday name:", wday[x])

Output

输出量

Weekday number for the given date: 3
Weekday number: 6
Weekday name: Sunday

Note: The date in the function argument should be valid, otherwise it will raise a ValueError.

注意:函数参数中的日期应有效,否则将引发ValueError

For example, if you print 31 September 2019, this will be wrong as September has only 30 days.

例如,如果您打印2019年9月31日 ,这将是错误的,因为9月只有30天。

Example:

例:

# Python program to illustrate the
# use of weekday() method
# importing calendar module
import calendar
year = 2019
month = 2
day = 29
x = calendar.weekday(year, month, day)
print("Weekday number for the given date:", x)
print()

Output

输出量

Traceback (most recent call last):
File "main.py", line 11, in <module>
x = calendar.weekday(year, month, day)
File "/usr/lib/python3.8/calendar.py", line 117, in w
eekday
return datetime.date(year, month, day).weekday()
ValueError: day is out of range for month

翻译自: https://www.includehelp.com/python/calendar-weekday-method-with-example.aspx

Python日历模块| 使用示例的weekday()方法相关推荐

  1. python中weekday_[转载] Python日历模块| 使用示例的weekday()方法

    参考链接: Python中的日历函数 2(monthrange(),prcal(),weekday()-) Python calendar.weekday()方法 (Python calendar.w ...

  2. [转载] Python日历模块| 使用示例的weekday()方法

    参考链接: Python中的日历函数 2(monthrange(),prcal(),weekday()-) Python calendar.weekday()方法 (Python calendar.w ...

  3. python日历模块_Python日历模块| firstweekday()方法与示例

    python日历模块 Python calendar.firstweekday()方法 (Python calendar.firstweekday() Method) firstweekday() m ...

  4. python日历模块_Python日历模块| setfirstweekday()方法与示例

    python日历模块 Python calendar.setfirstweekday()方法 (Python calendar.setfirstweekday() Method) setfirstwe ...

  5. python日历模块_Python日历模块| prmonth()方法与示例

    python日历模块 Python calendar.prmonth()方法 (Python calendar.prmonth() Method) prmonth() method is an inb ...

  6. python实现日历功能_详解Python日历模块的使用

    calendar模块的函数都是日历相关的,提供了对日期的一些操作方法,和生成日历的方法. calendar模块中提供了三大类: 一.calendar.Calendar(firstweekday=0) ...

  7. python gzip模块实现文件压缩的方法

    python gzip模块实现文件压缩的方法 使用gzip格式压缩文件,注意引入gzip包. 代码: 复制代码代码示例: #!/bin/python # #site: www.jbxue.com im ...

  8. Python日历模块| weekheader()方法与示例

    Python calendar.weekheader()方法 (Python calendar.weekheader() Method) weekheader() method is an inbui ...

  9. python日历模块提示没有属性月_Python calendar日历模块的说明

    calendar(日历)模块,默认每周第一天是星期一,最后一天是星期天. 函数及描述 1. calendar.calendar(year, w=2, l=1, c=6, m=3) 返回一个多行字符串格 ...

最新文章

  1. Java字符串的不可变性
  2. (转)浅谈HTML5与css3画饼图!
  3. RadioButton加入DataGrid模板列引起的问题。
  4. **kwargs特有的作用
  5. ad取消覆铜_【学院推荐】PCB工程师不得不看:超级实用AD常用快捷键总结
  6. python爬虫知识点总结(十九)Scrapy命令行详解
  7. Android——Fragment实例精讲——底部导航栏+ViewPager滑动切换页面
  8. 大数据学习笔记06:伪分布式Hadoop
  9. linux 6.4 安装oracle10g,Red Hat Linux 6.4 安装 Oracle 10g 及问题解决
  10. Java 序列化和反序列化
  11. 在linux下运行.o文件,Linux环境下在可执行程序中嵌入资源文件
  12. Windows Xp sp2 升级为 sp3
  13. 在DW中如何让代码对齐?
  14. Windows错误“ 0xc0000005”
  15. Gazebo models / Gazebo Error [Node.cc:90] No namespace found
  16. css中标准盒模型和怪异盒模型的区别,如何将标准盒模型转换为怪异盒模型
  17. 17AHU排位赛3 C题 (LCS)
  18. 联想thinkpad bios密码破解,t440,450,460,x240,250,260,x1c,yoga,y70,y50。。。。软件解密
  19. 【计算机组成与结构】中央处理器
  20. Classifier Guided Diffusion

热门文章

  1. 股妖APP也妖 -- 与暴风魔镜相关
  2. 最全shell脚本语句语法使用(超详细)
  3. vue项目--资产管理系统
  4. WPS在线预览,无需安装任何浏览器插件
  5. 35 款 JavaScript 图形图表库
  6. 智能合约:BUMO 智能合约开发(新接口)
  7. 《Adobe Fireworks CS6中文版经典教程》——导读
  8. Spring 框架的基础
  9. 闲鱼卖家近期卖出后纠纷4次,请谨慎交易怎么办?
  10. There is no Action mapped for namespace [/] and action name [] associated with context path [/dxm__s