Get Last Day of the Month in Python
Question: Is there a way using Python's standard library to easily determine (i.e. one
function call) the last day of a given month?

If the standard library doesn't support that, does the dateutil package
support this?


Answer: I didn't notice this earlier when I was looking at the [documentation for the
calendar module](https://docs.python.org/2/library/calendar.html), but a
method called
[monthrange](http://docs.python.org/library/calendar.html#calendar.monthrange)
provides this information:

> **monthrange(year, month)**  
>      Returns weekday of first day of the month and number of days in month,
> for the specified year and month.
    
    
    >>> import calendar
    >>> calendar.monthrange(2002,1)
    (1, 31)
    >>> calendar.monthrange(2008,2)
    (4, 29)
    >>> calendar.monthrange(2100,2)
    (0, 28)
    

so:

    
    
    calendar.monthrange(year, month)[1]
    

seems like the simplest way to go.

Just to be clear, `monthrange` supports leap years as well:

    
    
    >>> from calendar import monthrange
    >>> monthrange(2012, 2)
    (2, 29)
    

[My previous answer](http://stackoverflow.com/questions/42950/get-last-day-of-
the-month-in-python#43088) still works, but is clearly suboptimal.

