Python calendar.month() 方法
month() 方法是 Python 中日曆模塊的內置方法。它適用於簡單的文本日曆,並使用 TextCalendar 類的 formatmonth() 返回給定月份日曆的 multi-line 字符串表示形式。它類似於 prmonth() 函數,除了它返回一個字符串,其中 prmonth() 方法不返回任何內容。
模塊:
import calendar
用法:
month(year, month, w=0, l=0)
參數:
year
:它是一個必需參數,代表日曆的年份。month
:它是一個必需參數,代表日曆的月份。w
:它是一個可選參數,它指定居中的日期列的寬度。l
:它是一個可選參數,表示日曆中每周將使用的行數。
返回值:
這個方法的返回類型是<class 'str'>
(一個 multi-line 字符串)。該方法返回給定年份的月份的日曆。
例:
# Python program to illustrate the
# use of month() method
# importing calendar module
import calendar
# Printing May 2020 with column width=0
# and number of lines for each week=0
print("Printing May 2020 Calendar with default parameters")
print(calendar.month(2020, 5))
print()
print("Printing October 1980 Calendar with column width=4")
print(calendar.month(1980, 10, 4))
print()
print("Printing December 1999 Calendar with column width=5 and number of lines for each week=2")
print(calendar.month(1999, 12, 5, 2))
print()
calendar.setfirstweekday(4)
# First column on the left will be Friday
# Printing Jaunary 1950
print("Printing January 1950 with column width =5, lines per week = 2 and first column on the left as Friday")
print(calendar.month(1950, 1, 5, 2))
輸出
Printing May 2020 Calendar with default parameters May 2020 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Printing October 1980 Calendar with column width=4 October 1980 Mon Tue Wed Thu Fri Sat Sun 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Printing December 1999 Calendar with column width=5 and number of lines for each week=2 December 1999 Mon Tue Wed Thu Fri Sat Sun 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Printing January 1950 with column width =5, lines per week = 2 and first column on the left as Friday January 1950 Fri Sat Sun Mon Tue Wed Thu 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
相關用法
- Python calendar monthcalendar()用法及代碼示例
- Python calendar monthrange()用法及代碼示例
- Python calendar firstweekday()用法及代碼示例
- Python calendar weekday()用法及代碼示例
- Python calendar isleap()用法及代碼示例
- Python calendar setfirstweekday()用法及代碼示例
- Python calendar leapdays()用法及代碼示例
- Python calendar weekheader()用法及代碼示例
- Python calendar calendar()用法及代碼示例
- Python calendar prmonth()用法及代碼示例
- Python calendar prcal()用法及代碼示例
- Python callable()用法及代碼示例
- Python string capwords()用法及代碼示例
- Python Functools cached_property()用法及代碼示例
- Python cmath.isclose()用法及代碼示例
- Python cmp()用法及代碼示例
- Python compile()用法及代碼示例
- Python cmath.log10()用法及代碼示例
- Python cmath.asinh()用法及代碼示例
- Python OpenCV cv2.rectangle()用法及代碼示例
注:本文由純淨天空篩選整理自 Python calendar Module | month() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。