Creating a list of Mondays for a year

Discussion in 'Python' started by Chris, Sep 18, 2005.

  1. Chris

    Chris Guest

    Is there a way to make python create a list of Mondays for a given year?

    For example,

    mondays = ['1/3/2005','1/10/2005','1/17/2005','1/24/2005',
    '1/31/2005','2/7/2005', ... ]
    Chris, Sep 18, 2005
    #1
    1. Advertising

  2. Chris

    Guest

    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
    , Sep 18, 2005
    #2
    1. Advertising

  3. Chris

    Peter Hansen Guest

    Chris wrote:
    > Is there a way to make python create a list of Mondays for a given year?
    >
    > For example,
    >
    > mondays = ['1/3/2005','1/10/2005','1/17/2005','1/24/2005',
    > '1/31/2005','2/7/2005', ... ]


    from datetime import date, timedelta

    def mondays(year):
    '''generate all days that are Mondays in the given year'''
    jan1 = date(year, 1, 1)

    # find first Monday (which could be this day)
    monday = jan1 + timedelta(days=(7-jan1.weekday()) % 7)

    while 1:
    if monday.year != year:
    break
    yield monday
    monday += timedelta(days=7)

    >>> [str(x) for x in mondays(2005)]

    ['2004-01-05', '2004-01-12', ... '2004-12-27']

    Extension to support any day of the week (including renaming the
    function!) is left as an exercise to the reader. ;-)

    --
    Peter
    Peter Hansen, Sep 18, 2005
    #3
  4. "Chris" <> wrote:

    > Is there a way to make python create a list of Mondays for a given year?
    >
    > For example,
    >
    > mondays = ['1/3/2005','1/10/2005','1/17/2005','1/24/2005',
    > '1/31/2005','2/7/2005', ... ]


    Get the dateutil package (https://moin.conectiva.com.br/DateUtil):

    import dateutil.rrule as rrule
    from datetime import date

    mondays2005 = tuple(rrule.rrule(rrule.WEEKLY,
    dtstart=date(2005,1,1),
    count=52,
    byweekday=rrule.MO))

    George
    George Sakkis, Sep 18, 2005
    #4
  5. Chris

    Guest

    Consider also dateutil written by Gustavo Niemeyer
    and found at:
    https://moin.conectiva.com.br/DateUtil

    >>> from dateutil.rrule import *
    >>> list(rrule(WEEKLY, byweekday=MO, dtstart=date(2005,1,1), until=date(2005,12,31)))


    The library may be a little intimidating at first it is worth learning.

    waldek
    , Sep 19, 2005
    #5
  6. Chris

    Peter Hansen Guest

    George Sakkis wrote:
    > "Chris" <> wrote:
    >>Is there a way to make python create a list of Mondays for a given year?

    >
    > Get the dateutil package (https://moin.conectiva.com.br/DateUtil):
    >
    > import dateutil.rrule as rrule
    > from datetime import date
    >
    > mondays2005 = tuple(rrule.rrule(rrule.WEEKLY,
    > dtstart=date(2005,1,1),
    > count=52,
    > byweekday=rrule.MO))


    Count should probably be at least "53" to catch the years when there are
    that many Mondays.... such as 2001. Unfortunately, I suspect that will
    screw it up for other years where there are only 52 (but I don't know
    this dateutil package so someone who does would have to say for sure).

    -Peter
    Peter Hansen, Sep 19, 2005
    #6
  7. Chris

    Paul Rubin Guest

    Chris <> writes:
    > Is there a way to make python create a list of Mondays for a given year?
    > mondays = ['1/3/2005','1/10/2005','1/17/2005','1/24/2005',
    > '1/31/2005','2/7/2005', ... ]


    This is pretty inefficient but it's conceptually the simplest:

    def mondays(year):
    from calendar import weekday, monthrange
    return [('%d/%d/%d'%(month,day,year))
    for month in xrange(1,13)
    for day in xrange(1,1+monthrange(year,month)[1])
    if weekday(year,month,day) == 0]
    Paul Rubin, Sep 19, 2005
    #7
  8. "Peter Hansen" <> wrote:

    > George Sakkis wrote:
    > > "Chris" <> wrote:
    > >>Is there a way to make python create a list of Mondays for a given year?

    > >
    > > Get the dateutil package (https://moin.conectiva.com.br/DateUtil):
    > >
    > > import dateutil.rrule as rrule
    > > from datetime import date
    > >
    > > mondays2005 = tuple(rrule.rrule(rrule.WEEKLY,
    > > dtstart=date(2005,1,1),
    > > count=52,
    > > byweekday=rrule.MO))

    >
    > Count should probably be at least "53" to catch the years when there are
    > that many Mondays.... such as 2001. Unfortunately, I suspect that will
    > screw it up for other years where there are only 52 (but I don't know
    > this dateutil package so someone who does would have to say for sure).
    >
    > -Peter


    Sorry, my bad; waldek in the post below got it right. Here's yet
    another way that doesn't use the count or until keywords:

    >>> from itertools import takewhile
    >>> mondays2001 = tuple(takewhile(lambda d: d.year==2001,

    rrule.rrule(rrule.WEEKLY,
    dtstart=date(2001,1,1),
    byweekday=rrule.MO)))
    >>> print len(mondays2001)

    53

    George
    George Sakkis, Sep 19, 2005
    #8
  9. Chris

    Chris Guest

    Thanks to everyone for your help!

    That fit the need perfectly.

    In article <>,
    says...
    > Is there a way to make python create a list of Mondays for a given year?
    >
    > For example,
    >
    > mondays = ['1/3/2005','1/10/2005','1/17/2005','1/24/2005',
    > '1/31/2005','2/7/2005', ... ]
    >
    >
    Chris, Sep 19, 2005
    #9
  10. Chris

    Charles Krug Guest

    On Mon, 19 Sep 2005 12:10:04 GMT, Chris <> wrote:
    > Thanks to everyone for your help!
    >
    > That fit the need perfectly.
    >
    > In article <>,
    > says...
    >> Is there a way to make python create a list of Mondays for a given year?
    >>
    >> For example,
    >>
    >> mondays = ['1/3/2005','1/10/2005','1/17/2005','1/24/2005',
    >> '1/31/2005','2/7/2005', ... ]
    >>


    You can also calculate it using the Julian Day Number. Google on
    astronomical calculations, or read the introduction to one of the
    Numerical Recipies. . . books for a discussion and algorithm.
    Charles Krug, Sep 19, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGF2aWQ=?=

    Calendar control to display image on Mondays

    =?Utf-8?B?RGF2aWQ=?=, Jan 18, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    457
    Steven Cheng[MSFT]
    Jan 19, 2006
  2. Otuatail

    Week of year to full Year

    Otuatail, Dec 8, 2003, in forum: HTML
    Replies:
    4
    Views:
    963
    Toby A Inkster
    Dec 8, 2003
  3. David

    Calendar control to display image on Mondays

    David, Jan 18, 2006, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    129
    Steven Cheng[MSFT]
    Jan 19, 2006
  4. David

    Calendar control to display image on Mondays

    David, Jan 18, 2006, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    176
    Steven Cheng[MSFT]
    Jan 19, 2006
  5. Tom Meerstad

    calender starts on sundays instead of mondays

    Tom Meerstad, Oct 8, 2004, in forum: Javascript
    Replies:
    3
    Views:
    113
    Tom Meerstad
    Oct 11, 2004
Loading...

Share This Page