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


Python calendar yeardatescalendar()用法及代碼示例


日曆模塊允許輸出類似於程序的日曆,並提供與日曆相關的其他有用函數。 “日曆”模塊中定義的函數和類使用理想化的日曆,當前的公曆日曆在兩個方向上都無限期擴展。

Python中的yeardatescalendar()方法用於獲取一年中一個月中的整周列表。周列表中的條目是天數。這個月以外的天數為零。

用法: yeardatescalendar(year, width)

參數: 
year: year of the calendar
width: [Default:3] number of months in each row. 

返回: a list of month rows.

代碼1:


# Python program to demonstrate working 
# of yeardatescalendar() method 
  
# importing calendar module 
import calendar 
  
obj = calendar.Calendar() 
  
year = 2016
# default value of width is 3 
  
# priting with yeardatescalendar 
print(obj.yeardatescalendar(year))

輸出:

[[[[datetime.date(2018, 1, 1), datetime.date(2018, 1, 2), datetime.date(2018, 1, 3), datetime.date(2018, 1, 4), datetime.date(2018, 1, 5), datetime.date(2018, 1, 6), datetime.date(2018, 1, 7)], [datetime.date(2018, 1, 8), datetime.date(2018, 1, 9), datetime.date(2018, 1, 10), datetime.date(2018, 1, 11), datetime.date(2018, 1, 12), datetime.date(2018, 1, 13), datetime.date(2018, 1, 14)], [datetime.date(2018, 1, 15), datetime.date(2018, 1, 16), datetime.date(2018, 1, 17), datetime.date(2018, 1, 18), datetime.date(2018, 1, 19), datetime.date(2018, 1, 20), datetime.date(2018, 1, 21)], [datetime.date(2018, 1, 22), datetime.date(2018, 1, 23), datetime.date(2018, 1, 24), datetime.date(2018, 1, 25), datetime.date(2018, 1, 26), datetime.date(2018, 1, 27), datetime.date(2018, 1, 28)],
. . . . .
[datetime.date(2018, 12, 17), datetime.date(2018, 12, 18), datetime.date(2018, 12, 19), datetime.date(2018, 12, 20), datetime.date(2018, 12, 21), datetime.date(2018, 12, 22), datetime.date(2018, 12, 23)], [datetime.date(2018, 12, 24), datetime.date(2018, 12, 25), datetime.date(2018, 12, 26), datetime.date(2018, 12, 27), datetime.date(2018, 12, 28), datetime.date(2018, 12, 29), datetime.date(2018, 12, 30)], [datetime.date(2018, 12, 31), datetime.date(2019, 1, 1), datetime.date(2019, 1, 2), datetime.date(2019, 1, 3), datetime.date(2019, 1, 4), datetime.date(2019, 1, 5), datetime.date(2019, 1, 6)]]]]

請注意,輸出中的星期是七個datetime.date對象的列表。

代碼2:迭代周表

# Python program to demonstrate working 
# of yeardatescalendar() method 
  
# importing calendar module 
import calendar 
  
obj = calendar.Calendar() 
  
# iteratign with yeardatescalendar 
for day in obj.yeardatescalendar(2018, 1):
    print(day)

輸出:

[[[datetime.date(2018, 1, 1), datetime.date(2018, 1, 2), datetime.date(2018, 1, 3), datetime.date(2018, 1, 4) . . . datetime.date(2018, 1, 31), datetime.date(2018, 2, 1), datetime.date(2018, 2, 2), datetime.date(2018, 2, 3), datetime.date(2018, 2, 4)]]]
. . . .
[[[datetime.date(2018, 11, 26), datetime.date(2018, 11, 27), datetime.date(2018, 11, 28), datetime.date(2018, 11, 29), datetime.date(2018, 11, 30), datetime.date(2018, 12, 1), datetime.date(2018, 12, 2)], [datetime.date(2018, 12, 3). . .datetime.date(2018, 12, 29), datetime.date(2018, 12, 30)], [datetime.date(2018, 12, 31), datetime.date(2019, 1, 1), datetime.date(2019, 1, 2), datetime.date(2019, 1, 3), datetime.date(2019, 1, 4), datetime.date(2019, 1, 5), datetime.date(2019, 1, 6)]]]



相關用法


注:本文由純淨天空篩選整理自Shivam_k大神的英文原創作品 Python calendar module | yeardatescalendar() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。