1 解析日期为datetime对象
from dateutil.parser import parse
parse('2021-04-01')
parse('2021/04/01 12:13:14')
parse('Thu Apr 15 12:10:46 CST 2021')
2 生成时间段列表
import datetime
from dateutil.parser import parse
from dateutil import rrule
# 按天生成
list(rrule.rrule(rrule.DAILY, dtstart=parse('2021-02-27'), until=parse('2021-03-03')))
# [datetime.datetime(2021, 2, 27, 0, 0), datetime.datetime(2021, 2, 28, 0, 0), datetime.datetime(2021, 3, 1, 0, 0), datetime.datetime(2021, 3, 2, 0, 0), datetime.datetime(2021, 3, 3, 0, 0)]
# 按月生成
list(rrule.rrule(rrule.MONTHLY, dtstart=parse('2020-12-25'), until=datetime.datetime.now()))
[datetime.datetime(2020, 12, 25, 0, 0), datetime.datetime(2021, 1, 25, 0, 0), datetime.datetime(2021, 2, 25, 0, 0), datetime.datetime(2021, 3, 25, 0, 0)]