有没有办法让dateutils.rrule.rrule
能够及时运行?例如,我想
[dt.datetime(2014, 8, 29, 0, 0),
dt.datetime(2014, 9, 5, 0, 0),
dt.datetime(2014, 9, 12, 0, 0),
dt.datetime(2014, 9, 19, 0, 0),
dt.datetime(2014, 9, 26, 0, 0)]
在这个例子中:
import datetime as dt
from dateutil.rrule import rrule, WEEKLY
date0 = dt.datetime(2014, 10, 3)
date1 = dt.datetime(2014, 8, 26)
rrule(WEEKLY, dtstart=date0).between(date0, date1)
但是,这给了我一个空列表......
*插入悲伤,皱眉的脸
答案 0 :(得分:1)
import datetime as dt
import dateutil.rrule as RR
date0 = dt.datetime(2014, 10, 3)
date1 = dt.datetime(2014, 8, 26)
start = min([date0, date1])
end = max([date0, date1])
dow = RR.weekday(date0.weekday())
print(RR.rrule(RR.WEEKLY, byweekday=dow, dtstart=start).between(start, end))
产量
[datetime.datetime(2014, 8, 29, 0, 0),
datetime.datetime(2014, 9, 5, 0, 0),
datetime.datetime(2014, 9, 12, 0, 0),
datetime.datetime(2014, 9, 19, 0, 0),
datetime.datetime(2014, 9, 26, 0, 0)]
我认为有一种方法可以定义一个dtstart=date0
的套件,该套件在 date0
之前生成日期。您必须使用较早的日期。此外,必须使用between(a, b)
调用a <= b
,否则结果将为空。