2

有没有办法让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)

但是,这给了我一个空列表......

*插入悲伤、皱眉的脸*

4

1 回答 1

3
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,否则结果将为空。

于 2014-10-03T21:08:24.967 回答