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?
In the code below 'get_last_day_of_month(dt)' will give you this, with date in string format like 'YYYY-MM-DD'.
i have a simple solution:
If you don't want to import the
calendar
module, a simple two-step function can also be:Outputs:
Using
relativedelta
you would get last date of month like this:The idea is to get the fist day of month and use
relativedelta
to go 1 month ahead and 1 day back so you would get the last day of the month you wanted.Another solution would be to do something like this:
And use the function like this: