當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python Calendar itermonthdates()用法及代碼示例

Python Calendar.itermonthdates() 方法

Calendar.itermonthdates() 方法是 Python 中日曆模塊的 Calendar 類的內置方法。它使用此類的實例並返回給定年份中給定月份(1-12)的迭代器。迭代器將返回該月的所有天數(作為 datetime.date 對象)以及該月開始之前或月底之後的所有天數,這些天數需要獲得完整的一周。

模塊:

    import calendar

類:

    from calendar import Calendar

用法:

    itermonthdates(year, month)

參數:

  • year:代表日曆年
  • month:代表日曆的月份

返回值:

該函數返回給定年份的給定月份的迭代器。

例:

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

# import class
import calendar

# Creating Calendar Instance
cal = calendar.Calendar()
year = 2020
month = 2
# Complete weeks will be printed
for i in cal.itermonthdates(year, month):
    print(i)
print()

# Setting firstweekday value to 3
cal = calendar.Calendar(firstweekday = 3)
year = 2016
month = 2
for i in cal.itermonthdates(year, month):
    print(i)

輸出

2020-01-27
2020-01-28
2020-01-29
2020-01-30
2020-01-31
2020-02-01
2020-02-02
2020-02-03
2020-02-04
2020-02-05
2020-02-06
2020-02-07
2020-02-08
2020-02-09
2020-02-10
2020-02-11
2020-02-12
2020-02-13
2020-02-14
2020-02-15
2020-02-16
2020-02-17
2020-02-18
2020-02-19
2020-02-20
2020-02-21
2020-02-22
2020-02-23
2020-02-24
2020-02-25
2020-02-26
2020-02-27
2020-02-28
2020-02-29
2020-03-01

2016-01-28
2016-01-29
2016-01-30
2016-01-31
2016-02-01
2016-02-02
2016-02-03
2016-02-04
2016-02-05
2016-02-06
2016-02-07
2016-02-08
2016-02-09
2016-02-10
2016-02-11
2016-02-12
2016-02-13
2016-02-14
2016-02-15
2016-02-16
2016-02-17
2016-02-18
2016-02-19
2016-02-20
2016-02-21
2016-02-22
2016-02-23
2016-02-24
2016-02-25
2016-02-26
2016-02-27
2016-02-28
2016-02-29
2016-03-01
2016-03-02

參考:calendar.Calendar.itermonthdates



相關用法


注:本文由純淨天空篩選整理自 Python Calendar Class | itermonthdates() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。