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

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

    caimouse发表于 2015-12-11 22:30:28
    love 0

    本模块calendar提供了通用日期的相关操作函数。缺省情况下,星期一作为一周的第一天,星期天作为一周的最后一天。可以通过函数setfirstweekday()函数可以设置一周的第一天,比如参数输入6就是是星期天作为第一天了。

     

    class calendar.Calendar(firstweekday=0) 

    创建一个Calendar对象。 参数firstweekday是一个整数,指明一周开始的第一天是星期几。0表示星期一,6表示是星期天。

    例子:

    #python 3.4

    import calendar

     

    cal = calendar.Calendar(0)

    print(cal)

    结果输出如下:

    <calendar.Calendar object at 0x02A30E30>

     

    Calendar类有以下实例方法:

    iterweekdays() 

    返回一周的迭代对象,一般用来输出一周的每日的序号。默认是从0开始。

    例子:

    #python 3.4

    import calendar

     

    cal = calendar.Calendar(0)

    print(cal)

    print(cal.iterweekdays())

    for i in cal.iterweekdays():

        print(i)

    结果输出如下:

    <calendar.Calendar object at 0x02A50E30>

    <generator object iterweekdays at 0x02CEB8C8>

    0

    1

    2

    3

    4

    5

    6

     

    itermonthdates(year, month)

    返回指定年和月的相关日期,会从这个月指定周的第一天开始计算,和到这个月最后一周结束的一天。

    例子:

    #python 3.4

    import calendar

     

    cal = calendar.Calendar(0)

    print(cal)

    print(cal.itermonthdates(2015, 11))

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

        print(i, end = ',')

    结果输出如下:

    <calendar.Calendar object at 0x02D00E30>

    <generator object itermonthdates at 0x02D6B8C8>

    2015-10-26,2015-10-27,2015-10-28,2015-10-29,2015-10-30,2015-10-31,2015-11-01,2015-11-02,2015-11-03,2015-11-04,2015-11-05,2015-11-06,2015-11-07,2015-11-08,2015-11-09,2015-11-10,2015-11-11,2015-11-12,2015-11-13,2015-11-14,2015-11-15,2015-11-16,2015-11-17,2015-11-18,2015-11-19,2015-11-20,2015-11-21,2015-11-22,2015-11-23,2015-11-24,2015-11-25,2015-11-26,2015-11-27,2015-11-28,2015-11-29,2015-11-30,2015-12-01,2015-12-02,2015-12-03,2015-12-04,2015-12-05,2015-12-06,

     

    itermonthdays2(year, month) 

    返回一个月按星期排列的有效日期,以元组方式组合(日期,星期)。

    例子:

    #python 3.4

    import calendar

     

    cal = calendar.Calendar(0)

    print(cal)

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

        print(i, end = ',')

    结果输出如下:

    <calendar.Calendar object at 0x02CE0E30>

    (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),

     

    itermonthdays(year, month) 

    返回指定年和月的所有日期。

    例子:

    #python 3.4

    import calendar

     

    cal = calendar.Calendar(0)

    print(cal)

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

        print(i, end = ',')

    结果输出如下:

    <calendar.Calendar object at 0x02C80E30>

    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,

     

    monthdatescalendar(year, month) 

    返回指定年和月的所有日期,每周生成一个列表,再组成一个大列表返回。

    例子:

    #python 3.4

    import calendar

     

    cal = calendar.Calendar(0)

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

        print(i)

        print(';')

    结果输出如下:

    [datetime.date(2015, 10, 26), datetime.date(2015, 10, 27), datetime.date(2015, 10, 28), datetime.date(2015, 10, 29), datetime.date(2015, 10, 30), datetime.date(2015, 10, 31), datetime.date(2015, 11, 1)]

    ;

    [datetime.date(2015, 11, 2), datetime.date(2015, 11, 3), datetime.date(2015, 11, 4), datetime.date(2015, 11, 5), datetime.date(2015, 11, 6), datetime.date(2015, 11, 7), datetime.date(2015, 11, 8)]

    ;

    [datetime.date(2015, 11, 9), datetime.date(2015, 11, 10), datetime.date(2015, 11, 11), datetime.date(2015, 11, 12), datetime.date(2015, 11, 13), datetime.date(2015, 11, 14), datetime.date(2015, 11, 15)]

    ;

    [datetime.date(2015, 11, 16), datetime.date(2015, 11, 17), datetime.date(2015, 11, 18), datetime.date(2015, 11, 19), datetime.date(2015, 11, 20), datetime.date(2015, 11, 21), datetime.date(2015, 11, 22)]

    ;

    [datetime.date(2015, 11, 23), datetime.date(2015, 11, 24), datetime.date(2015, 11, 25), datetime.date(2015, 11, 26), datetime.date(2015, 11, 27), datetime.date(2015, 11, 28), datetime.date(2015, 11, 29)]

    ;

    [datetime.date(2015, 11, 30), datetime.date(2015, 12, 1), datetime.date(2015, 12, 2), datetime.date(2015, 12, 3), datetime.date(2015, 12, 4), datetime.date(2015, 12, 5), datetime.date(2015, 12, 6)]

    ;

     

    蔡军生  QQ:9073204  深圳



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