Creating a list of Mondays for a year

skip at pobox.com skip at pobox.com
Sun Sep 18 16:24:56 EDT 2005


    Chris> Is there a way to make python create a list of Mondays for a
    Chris> given year?  For example,

    Chris> mondays = ['1/3/2005','1/10/2005','1/17/2005','1/24/2005',
    Chris> '1/31/2005','2/7/2005',   ... ]

How about:

    import datetime

    oneday = datetime.timedelta(days=1)
    oneweek = datetime.timedelta(days=7)

    year = 2005

    start = datetime.date(year=year, month=1, day=1)
    while start.weekday() != 0:
        start += oneday

    days = []
    while start.year == year:
        days.append(start)
        start += oneweek

    print days

Skip



More information about the Python-list mailing list