#Auther : vivek sharma
#date : 06-11-2006
#Description : This script is used to get the list of week in given year, user have to input the year ,

def WeekFinderFromYear(year):
        """ will return all the week from selected year """
        import datetime


        month= MONTH['JANUARY']
        dt = datetime.date(year,month,1)
        dow_lst = []

        while dt.weekday() != day:
            dt = dt + datetime.timedelta(days=1)

        lst_month = MONTH.values()
        for mont in lst_month:
         while dt.month == mont:
            dt = dt + datetime.timedelta(days=7)

        #for each in dow_lst:
        #   print each
        return dow_lst

year=raw_input("Enter the year:")
week_list = WeekFinderFromYear(year)
for each in week_list:
	print each

Recommended Answers

All 2 Replies

What is the question?

So your script prints out all the mondays of a year, right?
Here's an easier version (well, maybe not really easier, but with less lines :))

import calendar

year = int(raw_input("Enter year: "))

for month in range(1, 13):
    mondays = [
        day.split()[0] for day in calendar.month(year, month).split("\n")[2:-1]
            if not day.startswith("  ")]
    for monday in mondays:
        print "%i-%02i-%02i" % (year, int(month), int(monday))

PS: I love list comprehensions :)

EDIT: You may have to write calendar.setfirstweekday(0) to make Monday the first day in the week. What is nice is that if you e.g. want to know all the Tuesdays of a year, just write calendar.setfirstweekday(1) .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.