[Tutor] Help with range of months spanning across years

Sean Carolan scarolan at gmail.com
Wed Feb 2 02:55:20 CET 2011


> This sounds somewhat like homework. If it is, that's fine, mention it,
> and we will help you. But we won't do your homework for you, so keep
> that in mind.

A reasonable assumption but this is actually going in a cgi tool that
I'm using at work.  The input comes from pull-down menus on a web
page.

Here's what I came up with, feel free to give suggestions on how this
might be made more efficient:

if startyear > endyear or (startyear == endyear and startmonth > endmonth):
    print 'Your start date must be earlier than the end date.'
else:
    datelist = []
    month = startmonth
    year = startyear
    while month != endmonth or year != endyear:
        datelist.append((year, month))
        if month == 12:
            month = 1
            year += 1
        else:
            month += 1
    datelist.append((year, month))
    print datelist

I was hoping there was some whiz-bang function that would just iterate
through months if it was fed a start and end date.  Can datetime or
calendar do this?


More information about the Tutor mailing list