1

I'm using Calendar and monthdatescalendar()

import Calendar

mycal = calendar.Calendar(0)
laborday = mycal.monthdatescalendar(2003, 9)[0][0]

correctly returns 09/01/2003

laborday = mycal.monthdatescalendar(2004, 9)[0][0]

returns 08/30/2014 which is not the desired result

What is the right way to correctly get FIRST ***day of the month (for any month/year) without getting the last ***day of the prior month instead because you get the first week correctly?

Vishal
  • 2,097
  • 6
  • 27
  • 45
  • possible duplicate of [Python: Find the date for the first Monday after a given a date](http://stackoverflow.com/questions/6558535/python-find-the-date-for-the-first-monday-after-a-given-a-date) –  Jul 08 '14 at 02:51

1 Answers1

2

The problem is that monthdatescalendar returns full weeks, so will include prior months if needed to pad the first week out.

To get the correct day, you'll need to check against the month of the first Monday datetime object and choose the following Monday if required.

import calendar

def getLaborDay(year):
    month = 9
    mycal = calendar.Calendar(0)
    cal = mycal.monthdatescalendar(year, month)
    if cal[0][0].month == month:
        return cal[0][0]
    else:
        return cal[1][0]

print getLaborDay(2003)
print getLaborDay(2004)

Gives:

2003-09-01
2004-09-06