473,787 Members | 2,881 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11179

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.timede lta(days=1)
oneweek = datetime.timede lta(days=7)

year = 2005

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

days = []
while start.year == year:
days.append(sta rt)
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.co m> 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.rru le(rrule.WEEKLY ,
dtstart=date(20 05,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(WEEK LY, byweekday=MO, dtstart=date(20 05,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.co m> 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.rru le(rrule.WEEKLY ,
dtstart=date(20 05,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.co m> 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+mont hrange(year,mon th)[1])
if weekday(year,mo nth,day) == 0]
Sep 19 '05 #7
"Peter Hansen" <pe***@engcorp. com> wrote:
George Sakkis wrote:
"Chris" <se***@yahoo.co m> 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.rru le(rrule.WEEKLY ,
dtstart=date(20 05,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(rru le.WEEKLY,
dtstart=date(20 01,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************ ************@ne ws2.atlantic.ne t>,
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.co m> wrote:
Thanks to everyone for your help!

That fit the need perfectly.

In article <MP************ ************@ne ws2.atlantic.ne t>,
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
2952
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 script created them. It used things like parentnode, insertBefore, appendChildNode,... Any ideas/direction is appreciated.
4
14154
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
2032
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. I want the user to enter the year then use a multi-select list box to choose the months not available, then go to a fresh screen, enter another year and choose the months not available for that year, and so on. In the table, there is one record...
2
1855
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
1975
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: YearID YEAR 1 2004 2 2005 3 2006 4 2007 PART OF ASP CODE IS:
3
4386
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 display this URL in the drop down list. The following is the code snippet I am using: $sql_query = mysql_query("SELECT DISTINCT year, semester, school, schoolurl FROM schoolproject_pics ORDER BY year");
7
6787
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 using a Query, combined two fields of the each table into one (Year Group & Group into GroupName). Using these fields, I have made a form for the Leaders so that when they input the Year Group they are in charge with and the specific group, a subform...
3
2259
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 wizard so I want to hand code the form. If this was any other language accessing an access form I know how to do it but I am not sure how to access the tables WITHIN the access database itself. Here is what I would like the form to do. On Load:...
2
2176
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. <?xml version="1.0" ?> <company> <turnover> <year id="2000">appi</year> <year id="2001">archana</year>
0
10169
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.