473,383 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Creating a list of Mondays for a year

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', ... ]

Sep 18 '05 #1
9 11113

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 '05 #2
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
Sep 18 '05 #3
"Chris" <se***@yahoo.com> 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

Sep 18 '05 #4
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 18 '05 #5
George Sakkis wrote:
"Chris" <se***@yahoo.com> 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
Sep 19 '05 #6
Chris <se***@yahoo.com> 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]
Sep 19 '05 #7
"Peter Hansen" <pe***@engcorp.com> wrote:
George Sakkis wrote:
"Chris" <se***@yahoo.com> 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

Sep 19 '05 #8
Thanks to everyone for your help!

That fit the need perfectly.

In article <MP************************@news2.atlantic.net>,
se***@yahoo.com 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', ... ]

Sep 19 '05 #9
On Mon, 19 Sep 2005 12:10:04 GMT, Chris <se***@yahoo.com> wrote:
Thanks to everyone for your help!

That fit the need perfectly.

In article <MP************************@news2.atlantic.net>,
se***@yahoo.com 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.

Sep 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: NotGiven | last post by:
I want to learn moer of what I saw in a recent example. They create a page that created new fields/element. It's not like they were hidden and they displayed them, they were not there, then the...
4
by: pankaj_wolfhunter | last post by:
hi, The Query to Create View is: CREATE VIEW PROJECT_vw AS \ SELECT \ PROJNO \ ,PROJNAME \ ,DEPTNO \ ,RESPEMP \ ,PRSTAFF \ ,CHAR(SUBSTR(monthname(PRSTDATE),1,3))||'
2
by: Ellen Manning | last post by:
I'm developing an A2K adp that contains doctor info and the months they are not available for teaching. A doctor can be unavailable for some months for one year and other months for another year. ...
2
by: David | last post by:
I am trying to get an image to appear on all Mondays within the calendar control. I also want that image to be a link. How can I do this?
15
by: Jack | last post by:
Hi, I have a asp form where one element is a list box which lists four years starting from 2004. This list is drawn from a database table which has YearID and Year as two fields as shown below:...
3
by: cpptutor2000 | last post by:
Could some PHP guru please help me? I am have a drop down list, whose options are read in dynamically from a table in a MySQL database. one of the items being read in is an URL. I am unable to...
7
by: ppywong | last post by:
Hi, hopefully people can help: I am creating a rather simple database for a kids holiday camp. I have two main tables: tblLData (Leaders info) and tblKData (kids info). What I have done is...
3
by: emajka21 | last post by:
Hello, and thank you in advance for trying to help me. I am trying to create an access 2000 form without using the wizard. It just doesn't seem like I can the level of complexity I want out of the...
2
by: pralhadkulkarni12 | last post by:
Hi, I am a new user of AJAX. I need to generate suggest list for every input given by user as we get in Google. To generate I need AJAX code which will read data following XML file. e.g....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.