+2 votes
in Programming Languages by (71.8k 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

+2 votes
by (71.8k points)
selected by
 
Best answer

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

Related questions

+3 votes
1 answer
+2 votes
1 answer
+2 votes
1 answer

...