Python Calendar.itermonthdays2() 方法
Calendar.itermonthdays2() 方法是內置的方法Calendar
類calendar
Python 中的模塊。它使用此類的實例並返回給定年份中給定月份的迭代器。返回的天數是元組,其中第一個值是一個月中的第一個值,第二個值是工作日數,即該日期的星期幾。該函數返回完整的周,即,一個月中的每個星期將有 7 個元組,而月外的日期在元組的第一個值中將有 0 個值。
例如,(0, 3) 這意味著它在該月之外並且今天是星期四。
模塊:
import calendar
類:
from calendar import Calendar
用法:
itermonthdays2(year, month)
參數:
year
:它是一個必需參數,它指定日曆的年份。month
:它是一個必需參數,它指定日曆的月份。
返回值:
這個方法的返回類型是<class 'generator'>
,它返回給定年份的給定月份的迭代器。返回的日期是一個元組,表示給定日期的日期和工作日。另外,請記住打印整周。
例:
# Python program to illustrate the
# use of itermonthdays2() method
# import class
import calendar
# Creating Calendar Instance
cal = calendar.Calendar()
year = 2009
month = 2
print("Iterating over the weeks of February 2009 where each tuple is date and weekday on that day")
print("Days outside the month are written as 0")
print("Iteration starts from weekday/second value as 0, ie, Monday")
for i in cal.itermonthdays2(year, month):
print(i)
# first value is the day of the month;
# days outside of the month is 0
# second value is weekday number where
# Monday is 0 till Sunday which is 6
print()
print()
# set the firstweekday to 4
cal = calendar.Calendar(firstweekday = 4)
year = 2020
month = 1
print("Iterating over the weeks of January 2020 where each tuple is date and weekday on that day")
print("Iteration starts from weekday/second value as 4, ie, Friday")
for i in cal.itermonthdays2(year, month):
print(i)
print()
print()
輸出
Iterating over the weeks of February 2009 where each tuple is date and weekday on that day Days outside the month are written as 0 Iteration starts from weekday/second value as 0, ie, Monday (0, 0) (0, 1) (0, 2) (0, 3) (0, 4) (0, 5) (1, 6) (2, 0) (3, 1) (4, 2) (5, 3) (6, 4) (7, 5) (8, 6) (9, 0) (10, 1) (11, 2) (12, 3) (13, 4) (14, 5) (15, 6) (16, 0) (17, 1) (18, 2) (19, 3) (20, 4) (21, 5) (22, 6) (23, 0) (24, 1) (25, 2) (26, 3) (27, 4) (28, 5) (0, 6) Iterating over the weeks of January 2020 where each tuple is date and weekday on that day Iteration starts from weekday/second value as 4, ie, Friday (0, 4) (0, 5) (0, 6) (0, 0) (0, 1) (1, 2) (2, 3) (3, 4) (4, 5) (5, 6) (6, 0) (7, 1) (8, 2) (9, 3) (10, 4) (11, 5) (12, 6) (13, 0) (14, 1) (15, 2) (16, 3) (17, 4) (18, 5) (19, 6) (20, 0) (21, 1) (22, 2) (23, 3) (24, 4) (25, 5) (26, 6) (27, 0) (28, 1) (29, 2) (30, 3) (31, 4) (0, 5) (0, 6) (0, 0) (0, 1) (0, 2) (0, 3)
相關用法
- Python Calendar itermonthdays3()用法及代碼示例
- Python Calendar itermonthdays4()用法及代碼示例
- Python Calendar itermonthdays()用法及代碼示例
- Python Calendar itermonthdates()用法及代碼示例
- Python Calendar iterweekdays()用法及代碼示例
- Python Calendar monthdatescalendar()用法及代碼示例
- Python Calendar monthdayscalendar()用法及代碼示例
- Python Calendar yeardatescalendar()用法及代碼示例
- Python Calendar yeardayscalendar()用法及代碼示例
- Python Calendar yeardays2calendar()用法及代碼示例
- Python Calendar monthdays2calendar()用法及代碼示例
- Python Condition release()用法及代碼示例
- Python Collections.UserString用法及代碼示例
- Python Condition notify()用法及代碼示例
- Python Condition wait()用法及代碼示例
- Python Sympy Curve.translate()用法及代碼示例
- Python Collections.UserDict用法及代碼示例
- Python Collections.UserList用法及代碼示例
- Python Condition notify_all()用法及代碼示例
- Python Condition acquire()用法及代碼示例
注:本文由純淨天空篩選整理自 Python Calendar Class | itermonthdays2() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。