每天CookBook之Python-054

  • 获取当前月的日期范围
  • 获取指定范围的日期范围
from datetime import datetime, date, timedelta
import calendar


def get_month_range(start_date=None):
    if start_date is None:
        start_date = date.today().replace(day=1)
    _, day_in_month = calendar.monthrange(start_date.year, start_date.month)
    end_date = start_date + timedelta(days=day_in_month)
    return start_date, end_date

a_day = timedelta(days=1)
first_day, last_day = get_month_range()
while first_day < last_day:
    print(first_day)
    first_day += a_day


def date_range(start, stop, step):
    while start < stop:
        yield  start
        start += step

for d in date_range(datetime(2012, 9, 1), datetime(2012, 10, 1), timedelta(hours=6)):
    print(d)

Out

2016-07-01
2016-07-02
2016-07-03
2016-07-04
2016-07-05
2016-07-06
2016-07-07
2016-07-08
2016-07-09
2016-07-10
2016-07-11
2016-07-12
2016-07-13
2016-07-14
2016-07-15
2016-07-16
2016-07-17
2016-07-18
2016-07-19
2016-07-20
2016-07-21
2016-07-22
2016-07-23
2016-07-24
2016-07-25
2016-07-26
2016-07-27
2016-07-28
2016-07-29
2016-07-30
2016-07-31
2012-09-01 00:00:00
2012-09-01 06:00:00
2012-09-01 12:00:00
2012-09-01 18:00:00
2012-09-02 00:00:00
2012-09-02 06:00:00
2012-09-02 12:00:00
2012-09-02 18:00:00
2012-09-03 00:00:00
2012-09-03 06:00:00
2012-09-03 12:00:00
2012-09-03 18:00:00
2012-09-04 00:00:00
2012-09-04 06:00:00
2012-09-04 12:00:00
2012-09-04 18:00:00
2012-09-05 00:00:00
2012-09-05 06:00:00
2012-09-05 12:00:00
2012-09-05 18:00:00
2012-09-06 00:00:00
2012-09-06 06:00:00
2012-09-06 12:00:00
2012-09-06 18:00:00
2012-09-07 00:00:00
2012-09-07 06:00:00
2012-09-07 12:00:00
2012-09-07 18:00:00
2012-09-08 00:00:00
2012-09-08 06:00:00
2012-09-08 12:00:00
2012-09-08 18:00:00
2012-09-09 00:00:00
2012-09-09 06:00:00
2012-09-09 12:00:00
2012-09-09 18:00:00
2012-09-10 00:00:00
2012-09-10 06:00:00
2012-09-10 12:00:00
2012-09-10 18:00:00
2012-09-11 00:00:00
2012-09-11 06:00:00
2012-09-11 12:00:00
2012-09-11 18:00:00
2012-09-12 00:00:00
2012-09-12 06:00:00
2012-09-12 12:00:00
2012-09-12 18:00:00
2012-09-13 00:00:00
2012-09-13 06:00:00
2012-09-13 12:00:00
2012-09-13 18:00:00
2012-09-14 00:00:00
2012-09-14 06:00:00
2012-09-14 12:00:00
2012-09-14 18:00:00
2012-09-15 00:00:00
2012-09-15 06:00:00
2012-09-15 12:00:00
2012-09-15 18:00:00
2012-09-16 00:00:00
2012-09-16 06:00:00
2012-09-16 12:00:00
2012-09-16 18:00:00
2012-09-17 00:00:00
2012-09-17 06:00:00
2012-09-17 12:00:00
2012-09-17 18:00:00
2012-09-18 00:00:00
2012-09-18 06:00:00
2012-09-18 12:00:00
2012-09-18 18:00:00
2012-09-19 00:00:00
2012-09-19 06:00:00
2012-09-19 12:00:00
2012-09-19 18:00:00
2012-09-20 00:00:00
2012-09-20 06:00:00
2012-09-20 12:00:00
2012-09-20 18:00:00
2012-09-21 00:00:00
2012-09-21 06:00:00
2012-09-21 12:00:00
2012-09-21 18:00:00
2012-09-22 00:00:00
2012-09-22 06:00:00
2012-09-22 12:00:00
2012-09-22 18:00:00
2012-09-23 00:00:00
2012-09-23 06:00:00
2012-09-23 12:00:00
2012-09-23 18:00:00
2012-09-24 00:00:00
2012-09-24 06:00:00
2012-09-24 12:00:00
2012-09-24 18:00:00
2012-09-25 00:00:00
2012-09-25 06:00:00
2012-09-25 12:00:00
2012-09-25 18:00:00
2012-09-26 00:00:00
2012-09-26 06:00:00
2012-09-26 12:00:00
2012-09-26 18:00:00
2012-09-27 00:00:00
2012-09-27 06:00:00
2012-09-27 12:00:00
2012-09-27 18:00:00
2012-09-28 00:00:00
2012-09-28 06:00:00
2012-09-28 12:00:00
2012-09-28 18:00:00
2012-09-29 00:00:00
2012-09-29 06:00:00
2012-09-29 12:00:00
2012-09-29 18:00:00
2012-09-30 00:00:00
2012-09-30 06:00:00
2012-09-30 12:00:00
2012-09-30 18:00:00
posted @ 2016-07-21 22:34  4Thing  阅读(87)  评论(0编辑  收藏  举报