+2 votes
in Programming Languages by (15.2k points)
I have to crate a report where I need to write only the last date of the month (e.g. jul- 31, jun-30). I can create a function to do this, but is there anything in python that can be used to achieve the goal?

1 Answer

0 votes
by (15.2k points)
edited by

Use monthrange function of the calendar module. It will give both start and end date for a given year and month. See the examples mentioned below.

>>> import calendar
>>> calendar.monthrange(2000,2)
(1, 29)
>>> calendar.monthrange(2000,2)[1]
>>> calendar.monthrange(2016,2)[1]
>>> calendar.monthrange(2015,2)[1]
>>> calendar.monthrange(2015,1)[1]