5.2 calendar--通用日期的相关函数(2)

monthdays2calendar(year, month)

返回指定年和月的所有日期,把日期和第几周组成元组。

例子:

#python 3.4

import calendar

 

cal = calendar.Calendar(0)

for i in cal.monthdays2calendar(2015, 11):

    print(i)

    print(';')

结果输出如下:

[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (1, 6)]

;

[(2, 0), (3, 1), (4, 2), (5, 3), (6, 4), (7, 5), (8, 6)]

;

[(9, 0), (10, 1), (11, 2), (12, 3), (13, 4), (14, 5), (15, 6)]

;

[(16, 0), (17, 1), (18, 2), (19, 3), (20, 4), (21, 5), (22, 6)]

;

[(23, 0), (24, 1), (25, 2), (26, 3), (27, 4), (28, 5), (29, 6)]

;

[(30, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6)]

;

 

monthdayscalendar(year, month)

按一周来返回指定年和月的日历。

例子:

#python 3.4

import calendar

 

cal = calendar.Calendar(0)

for i in cal.monthdayscalendar(2015, 11):

    print(i)

    print(';')

结果输出如下:

[0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0]

;

 

yeardatescalendar(year, width=3) 

返回一年里所有日期,并且按月按同排列。

例子:

#python 3.4

import calendar

 

cal = calendar.Calendar(0)

for i in cal.yeardatescalendar(2015, 10):

    print(i)

    print(';')

结果输出如下:

[[[datetime.date(2014, 12, 29), datetime.date(2014, 12, 30), datetime.date(2014, 12, 31), datetime.date(2015, 1, 1), datetime.date(2015, 1, 2), datetime.date(2015, 1, 3), 

...

 datetime.date(2015, 12, 27)], [datetime.date(2015, 12, 28), datetime.date(2015, 12, 29), datetime.date(2015, 12, 30), datetime.date(2015, 12, 31), datetime.date(2016, 1, 1), datetime.date(2016, 1, 2), datetime.date(2016, 1, 3)]]]

;

 

yeardays2calendar(year, width=3) 

返回指定年的所有日期,日期和一周第几天组成元组形式。

例子:

#python 3.4

import calendar

 

cal = calendar.Calendar(0)

for i in cal.yeardays2calendar(2015, 10):

    print(i)

    print(';')

结果输出如下:

[[[(0, 0), (0, 1), (0, 2), (1, 3), (2, 4), (3, 5), (4, 6)], [(5, 0), (6, 1), (7, 2), (8, 3), (9, 4), (10, 5), (11, 6)],

...

, (30, 2), (31, 3), (0, 4), (0, 5), (0, 6)]]]

;

 

yeardayscalendar(year, width=3) 

返回指定年的所有日期,每周组成一个列表,当不存在这个月以0代替。

例子:

#python 3.4

import calendar

 

cal = calendar.Calendar(0)

for i in cal.yeardayscalendar(2015):

    print(i)

    print(';')

结果输出如下:

[[[0, 0, 0, 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], 

...

 

class calendar.TextCalendar(firstweekday=0) 

这个类用来生成纯文本的日历。

例子:

#python 3.4

import calendar

 

cal = calendar.TextCalendar(0)

print(cal)

结果输出如下:

<calendar.TextCalendar object at 0x02C70E30>

 

formatmonth(theyear, themonth, w=0, l=0) 

返回指定年和月的所有日期,并按一周7天的方式排列。其中参数w表示列与列之间宽度,参数l表示行与行之间宽度。

例子:

#python 3.4

import calendar

 

cal = calendar.TextCalendar(0)

print(cal.formatmonth(2015, 11))

print(cal.formatmonth(2015, 11, w = 5))

print(cal.formatmonth(2015, 11, w = 0, l = 2))

结果输出如下:

   November 2015

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

 

              November 2015

 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

 

   November 2015

 

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

 

 

蔡军生 QQ:9073204 深圳

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

caimouse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值