python日历模块_Python日历模块| 带有示例的monthrange()方法

Python的calendar模块包含monthrange()方法,用于获取指定年份和月份的月初星期和月天数。返回值是一个元组,包含月初的星期编号和月总天数。
摘要由CSDN通过智能技术生成

python日历模块

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

monthrange() method is an inbuilt method of the calendar module in Python. It works on the simple text calendars and returns a tuple where the first value is the weekday of first day of the month and the second value is the number of days in month, for the specified year and month.

monthrange()方法是Python中日历模块的内置方法。 它适用于简单的文本日历,并返回一个元组,其中第一个值是指定年份和月份的月中第一天的工作日,第二个值是月中的天数。

Module:

模块:

    import calendar

Syntax:

句法:

    monthrange(year, month)

Parameter(s):

参数:

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

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

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

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

Return value:

返回值:

The return type of this method is <class 'tuple'>, where first value is the weekday number of the first day of the month and the second value is the number of days in that month.

此方法的返回类型为<class'tuple'> ,其中第一个值是该月第一天的工作日数,第二个值是该月中的天数。

Example:

例:

# Python program to illustrate the 
# use of monthrange() method

# importing calendar module
import calendar

year = 2020
month = 4

print("The first weekday number of the April 2020 and number of days in April:", calendar.monthrange(year, month))
print()
# Prints (2, 30) where 2 is the first 
# weekday number and 30 is the number of days in April

# We can store the values separately
year = 2010
month = 10
x, y = calendar.monthrange(year, month)
print("First weekday number:", x)
print("Number of days in this month:", y)
print()

# We can also print the WEEKDAY name
days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
year = 2019
month = 12
x, y = calendar.monthrange(year, month)
print("First weekday number", x)
print("Number of days in this month:", y)
print("Weekday was:", days[x])
print()

Output

输出量

The first weekday number of the April 2020 and number of days in April: (2, 30)

First weekday number: 4
Number of days in this month: 31

First weekday number 6
Number of days in this month: 31
Weekday was: Sunday


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

python日历模块

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值