Python 中的日历函数


Python 具有一个内置模块,称为 calendar,其操作与日历有关。Python 中有一些日历函数。

calendar(year, w, l, c)

该函数显示年份、字符宽度、每星期行数和列分隔符。

示例

print ("The calendar of 2014 is : ") 
print (calendar.calendar(2014,3,1,4))

输出

The calendar of year 2014 is : 
    2014
   January                        		February                  March
Mon Tue Wed Thu Fri Sat Sun    Mon Tue Wed Thu Fri Sat Sun    Mon Tue Wed Thu Fri Sat Sun
         1   2   3   4   5                          1   2                          1   2
 6   7   8   9  10  11  12      3   4   5   6   7   8   9     3   4   5   6   7    8   9
13  14  15  16  17  18  19     10  11  12  13  14  15  16     10  11  12  13  14  15  16
20  21  22  23  24  25  26     17  18  19  20  21  22  23     17  18  19  20  21  22  23
27  28  29  30  31             24  25  26  27  28             24  25  26  27  28  29  30
				                                                  31

      April                           	May                            	June
Mon Tue Wed Thu Fri Sat Sun    Mon Tue Wed Thu Fri Sat Sun    Mon Tue Wed Thu Fri Sat Sun
     1   2   3   4   5   6                  1   2   3   4                              1
 7   8   9  10  11  12  13      5   6   7   8   9  10  11      2   3   4   5   6   7   8
14  15  16  17  18  19  20     12  13  14  15  16  17  18      9  10  11  12  13  14  15
21  22  23  24  25  26  27     19  20  21  22  23  24  25     16  17  18  19  20  21  22
28  29  30                     26  27  28  29  30  31         23  24  25  26  27  28  29
                                                              30

      July                          	August                       	September
Mon Tue Wed Thu Fri Sat Sun    Mon Tue Wed Thu Fri Sat Sun    Mon Tue Wed Thu Fri Sat Sun
	  1   2   3   4   5   6                      1   2   3      1   2   3   4   5   6   7
 7   8   9  10  11  12  13      4   5   6   7   8   9  10      8   9  10  11  12  13  14
14  15  16  17  18  19  20     11  12  13  14  15  16  17     	15  16  17  18  19  20  21
21  22  23  24  25  26  27     18  19  20  21  22  23  24     	22  23  24  25  26  27  28
28  29  30  31                 25  26  27  28  29  30  31     	29  30

       October                       		 November                     December
Mon Tue Wed Thu Fri Sat Sun    Mon Tue Wed Thu Fri Sat Sun    Mon Tue Wed Thu Fri Sat Sun
         1   2   3   4   5                   	   1   2      1   2   3   4   5   6   7
6   7   8   9  10  11  12      3   4   5   6   7   8   9      8   9  10  11  12  13  14
13  14  15  16  17  18  19     10  11  12  13  14  15  16     15  16  17  18  19  20  21
20  21  22  23  24  25  26     17  18  19  20  21  22  23     22  23  24  25  26  27  28
27  28  29  30  31             24  25  26  27  28  29  30     29  30  31

firstweekday()

该函数返回星期的第一天。

示例

print ("The starting day in calendar is : ",end="") 
print (calendar.firstweekday()) 

输出

The starting day in calendar is :  0

isleap (year)

该函数检查参数中提到的年份是否为闰年。

示例

if (calendar.isleap(2014)): 
   print ("The year is leap year") 
else : 
   print ("The year is not leap year")

输出

The year is not leap year.

leapdays (year1, year2)

该函数计算参数中指定的年份之间的闰年(天)数。

示例

print ("The leap (year) days between 1950 and 2000 are : ",end="") 
print (calendar.leapdays(1950, 2000)) 

输出

The leap days between 1950 and 2000 are : 12

month (year, month, w, l)

该函数显示参数中提到的特定年份的月份。它采用 4 个参数:年份、月份、字符宽度和一周所占的行数。

示例

print ("The month 6th of 2017 is :") 
print (calendar.month(2017,6,3,1)) 

输出

The month 6th of 2017 is :
         June 2017
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

更新于:2019-07-30

1K+ 浏览

开启你的职业生涯

完成课程并获得认证

开始
广告