+2 votes
in Programming Languages by (10.5k 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 (10.5k 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]
29
>>> calendar.monthrange(2016,2)[1]
29
>>> calendar.monthrange(2015,2)[1]
28
>>> calendar.monthrange(2015,1)[1]
31

...