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

<p>If the standard library doesn't support that, does the dateutil package support this?</p>

<p>I didn't notice this earlier when I was looking at the <a href="https://docs.python.org/2/library/calendar.html">documentation for the calendar module</a>, but a method called <a href="http://docs.python.org/library/calendar.html#calendar.monthrange">monthrange</a> provides this information:</p>

<blockquote>
  <p><b>monthrange(year, month)</b><br>
  &nbsp;&nbsp;&nbsp;&nbsp;Returns weekday of first day of the month and number of days in month, for the specified year and month. </p>
</blockquote>

<pre><code>&gt;&gt;&gt; import calendar
&gt;&gt;&gt; calendar.monthrange(2002,1)
(1, 31)
&gt;&gt;&gt; calendar.monthrange(2008,2)
(4, 29)
&gt;&gt;&gt; calendar.monthrange(2100,2)
(0, 28)
</code></pre>

<p>so:</p>

<pre><code>calendar.monthrange(year, month)[1]
</code></pre>

<p>seems like the simplest way to go.</p>

<p>Just to be clear, <code>monthrange</code> supports leap years as well:</p>

<pre><code>&gt;&gt;&gt; from calendar import monthrange
&gt;&gt;&gt; monthrange(2012, 2)
(2, 29)
</code></pre>

<p><a href="http://stackoverflow.com/questions/42950/get-last-day-of-the-month-in-python#43088">My previous answer</a> still works, but is clearly suboptimal.</p>
