datetime.last_of
Return a datetime
set to be the last of some unit of time given by unit
. Acceptable values for unit
are "month"
, "quarter"
, and "year"
.
Week days may also be passed to this function. Setting the unit to 'month'
and the week day to 'monday
' will return the last Monday of the month, for example.
Times are stripped from the input datetime
.
Arguments:
def datetime.last_of(
dt: <datetime> = None,
unit: <str> = None, # 'quarter', 'month', or 'year'
day_of_week: <str> = None, # 'monday', 'tuesday', ...
):
Returns <datetime>
.
Example:
long_long_ago = datetime.from_date_and_time(
year=1995,
month=1,
day=15,
hour=0,
minute=0,
second=0)
# end_of_1995 == December 31 1995 23:59:59
end_of_1995 = datetime.last_of(dt=long_long_ago, unit='year')
# last_monday_1995 == Monday December 25 1995 00:00:00
last_monday_1995 = datetime.last_of(dt=long_long_ago, unit='year',
day_of_week='monday')