IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

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

    caimouse发表于 2015-12-12 19:25:58
    love 0

    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 深圳



沪ICP备19023445号-2号
友情链接