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?
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?
If you pass in a date range, you can use this:
Here is another answer. No extra packages required.
Get the first day of the next month and subtract a day from it.
If you want to make your own small function, this is a good starting point:
For this you have to know the rules for the leap years:
For me it's the simplest way:
I didn't notice this earlier when I was looking at the documentation for the calendar module, but a method called monthrange provides this information:
so:
seems like the simplest way to go.
Just to be clear,
monthrange
supports leap years as well:My previous answer still works, but is clearly suboptimal.