By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,551 Members | 2,622 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,551 IT Pros & Developers. It's quick & easy.

Finding all time periods for a given interval within a date range

P: n/a
Hi everyone,

I was wondering if anyone has written some Python code which uses a start
date and end date and a given interval (day, month or year) and outputs all
the time periods for that range and interval.

For example you may wish to find all the months between the dates
'2004-02-14' and '2004-08-04'. You would maybe use a function where you pass
in those starting and ending dates and the interval 'month' and you'd get
back a list of those months. I guess you would need to year part of the date
too, so ['2004-02', '2004-03', '2004-04', '2004-05', '2004-06', '2004-07',
'2004-08']. If you had passed in 'day' as the interval then you would be
given back a list of all the days in those months.

This is pretty specific but I would appreciate anything similar or even a
good pointing in the right direction of which modules to use. I've taken a
look at 'time' and 'calendar' but all I could see what the
'calendar.calendar' function which would require quite a bit of string
processing after getting the calendar strings.

Thanks for any help :-)
Graeme

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.732 / Virus Database: 486 - Release Date: 29/07/2004
__________________________________________________ ______________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
__________________________________________________ ______________________
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Graeme Longman wrote:
Hi everyone,

I was wondering if anyone has written some Python code which uses a start
date and end date and a given interval (day, month or year) and outputs all
the time periods for that range and interval.

For example you may wish to find all the months between the dates
'2004-02-14' and '2004-08-04'. You would maybe use a function where you pass
in those starting and ending dates and the interval 'month' and you'd get
back a list of those months. I guess you would need to year part of the date
too, so ['2004-02', '2004-03', '2004-04', '2004-05', '2004-06', '2004-07',
'2004-08']. If you had passed in 'day' as the interval then you would be
given back a list of all the days in those months.


Try using dateutil module by Gustavo Niemeyer found at:
https://moin.conectiva.com.br/DateUtil

from datetime import date
from dateutil.rrule import *
d1 = date(2004,2,14)
d2 = date(2004,8,4)
for d in rrule(MONTHLY, d1, until=d2):
print d.year, d.month

2004 2
2004 3
2004 4
2004 5
2004 6
2004 7

Substitute MONTHLY with DAILY in the rrule and you will get list of days

Waldek
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.