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日历模块