当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python calendar month()用法及代码示例


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 Module | month() Method with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。