日曆模塊允許輸出類似於程序的日曆,並提供與日曆相關的其他有用函數。 “日曆”模塊中定義的函數和類使用理想化的日曆,當前的公曆日曆在兩個方向上都無限期擴展。
Python中的monthdatescalendar()方法用於獲取一年中一個月中的整周列表。
用法: monthdatescalendar(year, month) 參數: year: year of the calendar month: month of the calendar 返回: a list of the weeks in the month.
代碼1:
# Python program to demonstrate working
# of monthdatescalendar() method
# importing calendar module
import calendar
obj = calendar.Calendar()
year = 2018
mnth = 9
# priting with monthdatescalendar
print(obj.monthdatescalendar(year, mnth))
輸出:
[[datetime.date(2018, 8, 27), datetime.date(2018, 8, 28), datetime.date(2018, 8, 29), datetime.date(2018, 8, 30), datetime.date(2018, 8, 31), datetime.date(2018, 9, 1), datetime.date(2018, 9, 2)], [datetime.date(2018, 9, 3), datetime.date(2018, 9, 4), datetime.date(2018, 9, 5), datetime.date(2018, 9, 6), datetime.date(2018, 9, 7), datetime.date(2018, 9, 8), datetime.date(2018, 9, 9)], [datetime.date(2018, 9, 10), datetime.date(2018, 9, 11), datetime.date(2018, 9, 12), datetime.date(2018, 9, 13), datetime.date(2018, 9, 14), datetime.date(2018, 9, 15), datetime.date(2018, 9, 16)], [datetime.date(2018, 9, 17), datetime.date(2018, 9, 18), datetime.date(2018, 9, 19), datetime.date(2018, 9, 20), datetime.date(2018, 9, 21), datetime.date(2018, 9, 22), datetime.date(2018, 9, 23)], [datetime.date(2018, 9, 24), datetime.date(2018, 9, 25), datetime.date(2018, 9, 26), datetime.date(2018, 9, 27), datetime.date(2018, 9, 28), datetime.date(2018, 9, 29), datetime.date(2018, 9, 30)]]
請注意,輸出中的星期是七個datetime.date對象的列表。
代碼2:迭代星期列表
# Python program to demonstrate working
# of monthdatescalendar() method
# importing calendar module
import calendar
obj = calendar.Calendar()
# iteratign with monthdatescalendar
for day in obj.monthdatescalendar(2018, 9):
print(day)
輸出:
[datetime.date(2018, 8, 27), datetime.date(2018, 8, 28), datetime.date(2018, 8, 29), datetime.date(2018, 8, 30), datetime.date(2018, 8, 31), datetime.date(2018, 9, 1), datetime.date(2018, 9, 2)]
[datetime.date(2018, 9, 3), datetime.date(2018, 9, 4), datetime.date(2018, 9, 5), datetime.date(2018, 9, 6), datetime.date(2018, 9, 7), datetime.date(2018, 9, 8), datetime.date(2018, 9, 9)]
[datetime.date(2018, 9, 10), datetime.date(2018, 9, 11), datetime.date(2018, 9, 12), datetime.date(2018, 9, 13), datetime.date(2018, 9, 14), datetime.date(2018, 9, 15), datetime.date(2018, 9, 16)]
[datetime.date(2018, 9, 17), datetime.date(2018, 9, 18), datetime.date(2018, 9, 19), datetime.date(2018, 9, 20), datetime.date(2018, 9, 21), datetime.date(2018, 9, 22), datetime.date(2018, 9, 23)]
[datetime.date(2018, 9, 24), datetime.date(2018, 9, 25), datetime.date(2018, 9, 26), datetime.date(2018, 9, 27), datetime.date(2018, 9, 28), datetime.date(2018, 9, 29), datetime.date(2018, 9, 30)]
相關用法
- Python calendar yeardatescalendar()用法及代碼示例
- Python calendar yeardays2calendar()用法及代碼示例
- Python calendar monthdayscalendar()用法及代碼示例
- Python calendar monthdays2calendar()用法及代碼示例
- Python calendar yeardayscalendar()用法及代碼示例
- Python calendar leapdays()用法及代碼示例
- Python calendar isleap()用法及代碼示例
- Python calendar firstweekday()用法及代碼示例
- Python calendar setfirstweekday()用法及代碼示例
- Python calendar itermonthdates()用法及代碼示例
- Python calendar formatyear()用法及代碼示例
- Python calendar prmonth()用法及代碼示例
- Python calendar itermonthdays()用法及代碼示例
- Python calendar pryear()用法及代碼示例
- Python calendar iterweekdays()用法及代碼示例
注:本文由純淨天空篩選整理自Shivam_k大神的英文原創作品 Python calendar module | monthdatescalendar() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。