How can I generate recurring dates using Python? For example I want to generate recurring date for "Third Friday of every second month". I want to generate recurring dates for daily, weekly, monthly, yearly (i.e., same as the recurrence function in Outlook Express).
-
1What code have you written so far? Please post the code you have written. – S.Lott Feb 19 '10 at 11:06
3 Answers
import dateutil.rrule as dr
import dateutil.parser as dp
import dateutil.relativedelta as drel
start=dp.parse("19/02/2010") # Third Friday in Feb 2010
This generates the third Friday of every month
rr = dr.rrule(dr.MONTHLY,byweekday=drel.FR(3),dtstart=start, count=10)
This prints every third Friday:
print map(str,rr)
# ['2010-02-19 00:00:00', '2010-03-19 00:00:00', '2010-04-16 00:00:00', '2010-05-21 00:00:00', '2010-06-18 00:00:00', '2010-07-16 00:00:00', '2010-08-20 00:00:00', '2010-09-17 00:00:00', '2010-10-15 00:00:00', '2010-11-19 00:00:00']
rr is an iterable, so you can use slicing notation to pick out every other item. This prints the third Friday of every other month:
print map(str,rr[::2])
# ['2010-02-19 00:00:00', '2010-04-16 00:00:00', '2010-06-18 00:00:00', '2010-08-20 00:00:00', '2010-10-15 00:00:00']
Above, I used str
to prettify the output a little bit. For more flexible string formatting of dates, use strftime
: See http://au2.php.net/strftime or the man page for strftime for all the options.
print [d.strftime('%d/%m/%Y') for d in rr[::2]]
# ['19/02/2010', '16/04/2010', '18/06/2010', '20/08/2010', '15/10/2010']

- 842,883
- 184
- 1,785
- 1,677
-
+1 because I shamelessly used your excellent example to answer this very similar SO question: http://stackoverflow.com/questions/3099007/date-library-capable-of-calculating-things-like-every-third-tuesday – mechanical_meat Jun 23 '10 at 05:20
You can give dateutil a try - especially its relativedelta and rrule fetures.

- 16,136
- 7
- 59
- 83
-
I use the following code: print list(rrule(DAILY, count=10,dtstart=parse("19/02/2010"))) The out put is looks like: [datetime.datetime(2010, 2, 19, 0, 0), datetime.datetime(2010, 2, 20, 0, 0), datetime.datetime(2010, 2, 21, 0, 0), datetime.datetime(2010, 2, 22, 0, 0), datetime.datetime(2010, 2, 23, 0, 0), datetime.datetime(2010, 2, 24, 0, 0), datetime.datetime(2010, 2, 25, 0, 0), datetime.datetime(2010, 2, 26, 0, 0), datetime.datetime(2010, 2, 27, 0, 0), datetime.datetime(2010, 2, 28, 0, 0)] But i want the output like: 19/02/2010 20/02/2010 21/02/2010 . . . Is it possible to change? – Nimmy Feb 19 '10 at 11:38
-
-
@nimmyliji: so you're not asking about recurring dates, but about date formatting. – Tomasz Zieliński Feb 19 '10 at 12:10
-
No. not like that..... I want to know about recurring date and also the date formatting. you told me to try the dateutil module..... After that only i tried to use dateutil – Nimmy Feb 19 '10 at 12:21
-
So please update the question with dateutil problem and update tags accordingly. – Tomasz Zieliński Feb 19 '10 at 12:44
you may try to write this yourself. you will first need an iterator which generates dates separated by a given interval:
import datetime
def dateiter(start, resolution):
date = start
while True:
yield date
date += resolution
now, you can generate dates and filter them:
# generate a list of every tuesday of february
# this iterates over every day from now, and filtered according to the rules
# warning: infinite generator below, there is nothing to end the iteration
tuesdays_of_february = (date for date in dateiter(datetime.datetime.now(), datetime.timedelta(days=1)) if date.weekday() == 4 and date.month == 2)
you can call the iterator yourself until you have enough dates:
>>> next(tuesdays_of_february)
datetime.datetime(2010, 2, 19, 14, 25, 46, 171000)
now, you need to limit the results:
>>> from itertools import *
>>>
>>> # get the five next valid dates:
>>> list(islice(tuesdays_of_february),5)
[datetime.datetime(2010, 2,26, 14, 25, 46, 171000), datetime.datetime(2011, 2, 4
, 14, 25, 46, 171000), datetime.datetime(2011, 2, 11, 14, 25, 46, 171000), datet
ime.datetime(2011, 2, 18, 1 4, 25, 46, 171000), datetime.datetime(2011, 2, 25
, 14, 25, 46, 171000)]
>>>
>>> # or until a condition is met:
>>> list(takewhile( lambda date: date.year < 2014, tuesdays_of_february ))
[datetime.datetime(2012, 2, 3, 14, 25, 46, 171000), datetime.datetime(2012, 2, 1
0, 14, 25, 46, 171000), datetime.datetime(2012, 2, 17, 14, 25, 46, 171000), date
time.datetime(2012, 2, 24, 14, 25, 46, 171000), datetime.datetime(2013, 2, 1, 14
, 25, 46, 171000), datetime.datetime(2013, 2, 8, 14, 25, 46, 171000), datetime.d
atetime(2013, 2, 15, 14, 25, 46, 171000), datetime.datetime(2013, 2, 22, 14, 25,
46, 171000)]
don't forget to have a look at the documentation for the datetime
module.

- 22,486
- 6
- 42
- 73