我是Python新手,也是dateutil
模塊。我傳遞了以下參數:dateutils rrule返回相隔2個月的日期
disclosure_start_date = resultsDict['fd_disclosure_start_date']
disclosure_end_date = datetime.datetime.now()
disclosure_dates = [dt for dt in rrule(MONTHLY, dtstart=disclosure_start_date, until=disclosure_end_date)]
這裏其轉換爲datetime是datetime.datetime(2012, 10, 31, 0, 0)
結束日期是截至目前。
當我使用:
disclosure_dates = [dt for dt in rrule(MONTHLY, dtstart=disclosure_start_date, until=disclosure_end_date)]
我得到每隔一個月的日期或2個月外。結果是:
>>> list(disclosure_dates)
[datetime.datetime(2012, 10, 31, 0, 0),
datetime.datetime(2012, 12, 31, 0, 0),
datetime.datetime(2013, 1, 31, 0, 0),
datetime.datetime(2013, 3, 31, 0, 0),
datetime.datetime(2013, 5, 31, 0, 0),
datetime.datetime(2013, 7, 31, 0, 0),
datetime.datetime(2013, 8, 31, 0, 0),
datetime.datetime(2013, 10, 31, 0, 0),
datetime.datetime(2013, 12, 31, 0, 0),
datetime.datetime(2014, 1, 31, 0, 0),
datetime.datetime(2014, 3, 31, 0, 0),
datetime.datetime(2014, 5, 31, 0, 0),
datetime.datetime(2014, 7, 31, 0, 0),
datetime.datetime(2014, 8, 31, 0, 0),
datetime.datetime(2014, 10, 31, 0, 0),
datetime.datetime(2014, 12, 31, 0, 0),
datetime.datetime(2015, 1, 31, 0, 0),
datetime.datetime(2015, 3, 31, 0, 0),
datetime.datetime(2015, 5, 31, 0, 0),
datetime.datetime(2015, 7, 31, 0, 0),
datetime.datetime(2015, 8, 31, 0, 0),
datetime.datetime(2015, 10, 31, 0, 0),
datetime.datetime(2015, 12, 31, 0, 0),
datetime.datetime(2016, 1, 31, 0, 0),
datetime.datetime(2016, 3, 31, 0, 0),
datetime.datetime(2016, 5, 31, 0, 0)]
我不知道我在做什麼錯。有人能指出這裏的錯誤嗎?
FYI,有[RFC 7529](https://tools.ietf.org/html/rfc7529),其(尤其)通過回退到一個模式延伸'RRULE'如果一天不存在,則在一個月的最後一天。規則'FREQ = MONTHLY; RSCALE = GREGORIAN; SKIP = BACKWARD; BYMONTHDAY = 30'會這樣做。今年2月,這將導致第28位(或閏年第29位)。不幸的是,目前還沒有太多的支持。 – Marten
@Marten真棒,不知道。我會閱讀並實施dateutil。 – Paul
謝謝保羅。 upvoted你的答案。 – DrBug