-4

How do I calculate the last days of a month?

For example, I have to create an IF statement that determines if the returned date lands on the last weekend of the month: then it needs to skip back to the last business day of the month. I cant put in something like 31 or 30 because if the 31st lands on a business day then it would be pointless and mess up my function.

Only in the last week of the month. I need to find whether the given date(without hardcoding) is on a weekend. If its on a weekend I need to find out if it lands on the last weekend of the month. If it lands on the last weekend of the month then I need to go back. Else, I need to go forward. This is not a duplicate.

All help is appreciated. Thank you.

Bruno
  • 487
  • 2
  • 8
  • 17
  • 2
    Possible duplicate of [which day of week given a date python](http://stackoverflow.com/questions/9847213/which-day-of-week-given-a-date-python) – pvg Mar 20 '17 at 19:21
  • 1
    As has often been said, Stack Overflow is not a code writing service. If you look at the docs, you will see standard libraries for doing computations with dates. Once you figure out on what day of the week the month ends, the rest should be straightforward. – Tom Zych Mar 20 '17 at 19:29

1 Answers1

-1

I would recommend looking into the datetime library. Suppose you want to know what day the 31st of January, 2014 was. You would do this

import datetime
mydate=datetime.date(2014,01,31)
mydate.weekday()

the datetime.date.weekday() method returns an integer from 0 to 6, where 0 is a Monday. The above code will give you a 4, meaning that January 31st, 2014 was a Friday.

Allan B
  • 329
  • 2
  • 9