Start New Discussion within our Software Development Community

The assumption is, that the typical workweek starts with Monday and ends with Friday. This is something the average working bloke has to live with (unless you work for a bank or the government). This little code will spit out a listing of all the workdays of a given month. It does not consider special holidays during the week. Anyway, the module calendar that comes with Python does a swell job.

# print out all the workdays (Mon - Fri) of a month
# tested with Python24    vegaseat     01feb2007

import calendar

# change these values to your needs ...
year = 2007
month = 2     # jan=1

# makes Monday first day of week (this is the default)
calendar.setfirstweekday(calendar.MONDAY)

months = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
          'August', 'September', 'October', 'November', 'December']
days2 = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su']

print "All the workdays in %s %d:" % (months[month-1] , year)
k = 0
for day in range(1, 32):
    try:
        weekday = calendar.weekday(year, month, day)
    except ValueError:
        continue
    if weekday < calendar.SATURDAY:
        k += 1
        # format the result
        print "%2d) %s  %02d/%02d/%d" % (k, days2[weekday], month, day, year)

"""
my output -->
All the workdays in February 2007:
 1) Th  02/01/2007
 2) Fr  02/02/2007
 3) Mo  02/05/2007
 4) Tu  02/06/2007
 5) We  02/07/2007
 6) Th  02/08/2007
 7) Fr  02/09/2007
 8) Mo  02/12/2007
 9) Tu  02/13/2007
10) We  02/14/2007
11) Th  02/15/2007
12) Fr  02/16/2007
13) Mo  02/19/2007
14) Tu  02/20/2007
15) We  02/21/2007
16) Th  02/22/2007
17) Fr  02/23/2007
18) Mo  02/26/2007
19) Tu  02/27/2007
20) We  02/28/2007
"""
The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.